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

2)Allure報告

3)Swagger界面

二、代碼分布結(jié)構(gòu)如下:
-
allure_report:allure生成的報告
- data:存儲數(shù)據(jù)
- html:生成html
-
biz_service: 一些常用的服務(wù)
- funding_serivce.py:放款服務(wù)
- mysql_service.py:MySQL連接服務(wù)
- read_env_service.py:讀取環(huán)境服務(wù)
- redis_service.py:Redis連接服務(wù)
-
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請求
-
db_dal:
- db_model:通過sqlacodegen,把已存在的數(shù)據(jù)庫模型轉(zhuǎn)為sqlalchemy的模型
env_config:各環(huán)境配置
log:記錄執(zhí)行接口測試后生成的log
-
project_interface:各個項目下的接口及數(shù)據(jù)
- data:具體接口需要用到的數(shù)據(jù)
- enums:具體項目需要的枚舉
- interface:具體接口
resource_files:需要上傳的資源文件
swagger:通過flask_restplus生成swagger頁面,直接通過URL執(zhí)行用例
test_cases:測試用例
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