[原] (04)python_interface_auto_demo04接口自動化

api_auto_v2

git地址如下:https://github.com/ellaann/python_interface_auto/tree/master/api_auto_v2

一、實現(xiàn)功能如下:

1、api_auto_v2功能

  1. 對request進(jìn)行封裝,實現(xiàn)get/post請求;
  2. 運(yùn)行pytest框架,對相關(guān)接口進(jìn)行參數(shù)化測試;
  3. 保存讀取cookie,方便后續(xù)接口使用;
  4. 支持json、圖片格式作為body發(fā)送post請求;
  5. 關(guān)聯(lián)Mysql數(shù)據(jù)庫,并通過sqlalchemy進(jìn)行數(shù)據(jù)的增改查;
  6. 關(guān)聯(lián)Redis數(shù)據(jù)庫對數(shù)據(jù)進(jìn)行查找、刪除;
  7. 根據(jù)不同的測試環(huán)境,選擇host環(huán)境配置進(jìn)行運(yùn)行;
  8. 生成Allure報告;
  9. 生成swagger界面,直接通過接口調(diào)用方法;
  10. 生成log,查看運(yùn)行記錄及后臺返回;
  11. 添加身份證、手機(jī)號碼生成;

2、功能界面

1)Log記錄

image.png

2)Allure報告

image.png

3)Swagger界面

image.png

二、代碼分布結(jié)構(gòu)如下:

  1. allure_report:allure生成的報告

    • data:存儲數(shù)據(jù)
    • html:生成html
  2. biz_service: 一些常用的服務(wù)

    • funding_serivce.py:放款服務(wù)
    • mysql_service.py:MySQL連接服務(wù)
    • read_env_service.py:讀取環(huán)境服務(wù)
    • redis_service.py:Redis連接服務(wù)
  3. common:公用文件夾,如短信、簽約、貸后等

    • enums:常用的枚舉
    • utils:常用的工具
      • config_headers_data.py.py:有關(guān)headers頭的數(shù)據(jù)
      • generator_initial_data.py:生成初始數(shù)據(jù)
      • logger.py:生成的日志設(shè)定
      • request.py:封裝request請求
  4. db_dal:

    • db_model:通過sqlacodegen,把已存在的數(shù)據(jù)庫模型轉(zhuǎn)為sqlalchemy的模型
  5. env_config:各環(huán)境配置

  6. log:記錄執(zhí)行接口測試后生成的log

  7. project_interface:各個項目下的接口及數(shù)據(jù)

    • data:具體接口需要用到的數(shù)據(jù)
    • enums:具體項目需要的枚舉
    • interface:具體接口
  8. resource_files:需要上傳的資源文件

  9. swagger:通過flask_restplus生成swagger頁面,直接通過URL執(zhí)行用例

  10. test_cases:測試用例

  11. select_env.py:執(zhí)行環(huán)境選擇和主文件路徑

三、需要用到指令

1、更新數(shù)據(jù)庫對象

sqlacodegen使用方法----把已存在的數(shù)據(jù)庫模型轉(zhuǎn)為sqlalchemy的模型
如有數(shù)據(jù)庫表或者字段更新,更進(jìn)入db_dal/db_model路徑下cmd執(zhí)行以下命令,會自動生成(不加--table 表名參數(shù)表示整個庫都會生成)

1) 導(dǎo)出整個數(shù)據(jù)庫

sqlacodegen --outfile=models.py mysql://root:password@127.0.0.1:3306/test_db
sqlacodegen --outfile=supply_chain_mapper.py mysql://this_is_username:this_is_password@127.0.0.19:3306/supplychain?charset=utf8

2) 導(dǎo)出某些表

格式:sqlacodegen 文件名 mysql://用戶名:密碼@IP:PORT/庫名稱?charset=utf8 --tables bs_white_list, sc_brand_info
例如:sqlacodegen --outfile supply_chain_model.py mysql://this_is_username:this_is_password@127.0.0.19:3306/supply?charset=utf8 --tables bs_white_lis
sqlacodegen --outfile=supply_chain_model.py mysql://this_is_username:this_is_password@127.0.0.19:3306/supply?charset=utf8 --tables sc_brand_info

2、生成allure報告

pytest -s -q --alluredir ./report/xml
allure generate report/xml -o report/html --clean

1) 先執(zhí)行pytest,產(chǎn)生allure報告需要的數(shù)據(jù)

python -m pytest --alluredir=allure_report/data test_cases/supply_g31/g31_user/test_borrower_auth_sms_code_interface.py

2)執(zhí)行allure,生成allure報告,到指定目錄

allure generate allure_report/data -o allure_report/html --clean

3、涉及到的庫

使用pipreqs生成requirements.txt ,在需要生成的目錄下執(zhí)行:

pipreqs ./ --encoding=utf-8 --force

安裝requirements.txt依賴

pip install -r requirements.txt

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容