这是一个采用pytest框架,结合allure完成接口自动化测试的项目,最后采用allure生成直观美观的测试报告,由于添加了allure的特性,使得测试报告覆盖的内容更全面和阅读起来更方便。
- 使用pytest构建测试框架,首先配置好pytest.ini, 它会决定pytest的运行方式。
pytest.ini放在项目根目录下,名字不能改做其他。

如上,pytest中配置了很多数据,包括命令行的参数,收集测试用例的目录和方式,日志显示的级别和方式以及形式。可以配置markers: ,不过我项目中用不着,就没配置。
- 参数化和接口依赖部分:使用了conftest.py来配置fixture, 放在根目录下,对项目下所有测试都覆盖。

- 测试用例部分,测试文件名注意以test开头,测试方法命名以test_开头,我的测试用例是存放在test文件夹下的.
测试用例上加了很多allure的特性,包括allure.story, allure.title, allure.severity, 其中allure.title部分做了参数化,可以动态生成测试标题,allure.severity这个部分很重要,能依据功能的重要程度去标记测试用例如果出现bug的严重等级,它的写法可以如下图在()内写入"normal", "critical", "blocker","minor", "trivial"这5个等级,默认是normal. 也可以这么写:在()内写入allure.severity_level.CRITICAL.

- 接着,我们用命令行去执行测试,因为要结合allure输出allure报告,所以命令是
pytest -n auto --alluredir=allure

然后,窗口就返回执行的结果,执行了多少个用例,每个用例的执行结果

再看看allure文件夹下生成了一堆json, txt 文件,这并不是我们想要的,怎么生成美观的allure测试报告呢?

- 还需要最后一步,输入 allure serve allure, 它会生成一个allure测试报告

这样就生成了,美观直观的allure测试报告,上面有用例重要程度的图表。