- 装置pytest
pytest是python的一个第三方单元测试框架,在这里用于生成原始的执行后果。
命令行或者终端中输出 pip install pytest 即可进行装置。
- 装置allure-pytest
allure-pytest是python的一个第三方库。用于连贯pytest和allure,使它们能够配合在一起应用。
allure-pytest基于pytest的原始执行后果生成实用于allure的json格局后果。该json格局后果能够用于后续实用allure生成html后果。
命令行或者终端中输出 pip install allure-pytest 即可进行装置。
-
装置allure
这个工具次要用来把测试用例的运行后果转换成html格局
能够去GitHub上下载:https://github.com/allure-fra…
在这里插入图片形容
下载后解压,把bin目录增加至path环境变量代码演示
代码如下,test_allure_demo.py
# encoding: utf-8 """ @File : test_allure_demo.py @Author : 灵枢 @Time : 2020/4/13 5:05 PM @Desc : """ import allure @allure.step("步骤1:关上百度") def step_1(): print("111") @allure.step("步骤2:输出关键字") def step_2(): print("222") @allure.feature("搜寻") class TestEditPage(): @allure.story("百度搜寻") def test_1(self): '''这是测试百度搜寻''' step_1() step_2() print("百度一下,你就晓得") @allure.story("谷歌搜寻") def test_2(self): '''这是测试谷歌搜寻''' assert 1 == 2, "搜寻失败"
在PyCharm的Terminal窗口运行:
先切换到测试代码的目录下,而后执行命令:
pytest test_allure_demo.py --alluredir ./report
-
生成报告
a. 关上终端terminal,切到测试文件所在目录。
b. 生成json格局运行后果运行命令 pytest --alluredir=report
命令中的 –alluredir=report 指明了生成的json后果文件寄存的目录为当前目录下的report文件夹
基于pytest捕捉到的测试用例,每个用例的执行后果会生成一个json文件。如下图所示:c. 应用allure生成最终的测试报告
运行命令 allure generate report。
这个命令会将 report 文件夹下的json文件渲染成网页后果,不便观看。生成的网页后果默认保留在以后文件夹下的 allure-report 文件夹内。
- 关上生成的网页报告
allure-report 文件夹,这里的 index.html 就是最终的后果页面。但间接通过浏览器关上这个文件是看不到理论内容的,这是因为理论内容须要 allure 进行渲染后能力看到。间接关上index.html,浏览器窗口如下图所示:
想要看到理论内容,须要应用allure内置的命令。allure应用了两种形式来渲染页面。别离是allure open 和 allure serve。前者用于在本地渲染和查看后果,后者用于在本地渲染后对外展现后果。这里咱们应用allure open。运行命令 allure open allure-report即可主动关上浏览器展现渲染好的后果。这里的allure-report为allure generate生成的后果所在目录。