python+unittest接口自动化测试框架整理~~~整体框架

经过一段时间的使用,决定重新整理接口测试框架,整体结构如下:

           

common下文件最多,分别为:case_template 测试用例模版、handle_config 配置、handle_data 数据替换、handle_db 数据库操作、handle_del 文件删除、handle_excel表格读写、handle_logger 日志、request_http http请求、request_webservice webservice请求、send_email 邮件发送等模块。

conf 下存放 test.conf 配置文件、constant_path 路径文件;

libs 下存放外部引入的模块,例如: ddt 、HTMLTestRunnerNew;

logs 下存放日志文件,如api_test_2020011418.log;

testdatas下存放测试数据,如 wsf.xlsx,表格中分sheet存放不同类别接口测试用例数据;

testcases下存放一个test_apicases.py;

testreports下存放html测试报告,如API_TestReport.html

基础执行步骤:

1. 运行handle_excel: 从测试数据wsf.xlsx中逐行读取测试数据,并处理为[ {'case_id': 1, 'moudle': 'caccount', 'title': '游客token', 'method': 'get', 'url': '/caccount/token', 'param': '{}', 'expect': "{'success': True}", 'actual': "", 'result': ''"},{}....] 这种格式;

2. 运行handle_data: 通过正则匹配,替换url中的路径参数如/caccount/info?token={token},以及param中的关联参数--setup中添加到类属性,执行用例前获取后进行替换;

3. 运行request_http:发送请求,将请求结果中的实际值与期望值使用assert断言对比,如果还需核对数据库数据,运行handle_db,assert 请求前后数据库数据的变化是否与预期一致;

4. 将返回的请求结果写回excel中;

改进点:

1. 由于不同类别接口测试用例代码重复度较高,因此提取出共同的部分,作为case_template,然后在test_apicases中继承测试用例模板;

2. 将BeautifulReport整个文件夹放入site-packages目录下,使用BeautifulReport,优化了接口测试报告模版。此处针对网上的BeautifulReport源码修改了一下,使测试报告路径与日志路径分开;

3. 对run.py 封装为类,可根据传的参数不同 运行不同类别的接口集,打算后期配合 jenkins参数化构建使用;

4. 新增handle_del,控制日志和报告目录下文件不得超出10个,超过时自动删除最早的;

测试报告效果图:

  

    

原文地址:https://www.cnblogs.com/qingyuu/p/12197823.html