Pytest+Allure环境的搭建
1. pytest的安装:
1.1. windows下:
pip install pytest
1.2. linux下:
pip install pytest
2. 安装pytest-allure-adaptor插件
2.1. windows下:
pip install pytest-allure-adaptor
3. allure的安装:
3.1. windows下:
前情提示: allure是基于Java的一个程序,需要Java1.8的环境,没有安装需要去安装一下。
下载之后,将压缩包解压到一个磁盘中,我这里用的是F盘

image
3.2. 配置allure的环境变量

image

image
点击确定,保存。这样就可以通过CMD使用allure命令
3.3. 编写测试文件
pycharm新建一个test_demo.py文件,代码如下:
import allure
@allure.MASTER_HELPER.feature("测试Dome")
class TestDome(object):
@allure.MASTER_HELPER.step("定义被测函数")
def func(self, x):
return x+1
@allure.MASTER_HELPER.story("被测场景")
@allure.MASTER_HELPER.severity("blocker")
@allure.MASTER_HELPER.step("断言结果")
def test_func(self):
# with allure.MASTER_HELPER.step("断言结果"):
allure.MASTER_HELPER.attach("预期结果", "{}".format(self.func(3)))
allure.MASTER_HELPER.attach("实际结果", "{}".format(5))
assert self.func(3) == 5
3.4. 生成测试报告
在pycharm中打开terminal

image
输入命令pytest -s --alluredir=report,会遇到以下这个错误:

image
进入allure下面的utils文件,修改以下代码:
# utils文件,可以通过from allure import utlis进入
for suitable_name in suitable_names:
# markers.append(item.get_marker(suitable_name))
markers.append(item.get_closest_marker(suitable_name))

image
修改之后,再次运行pytest -s --alluredir=report命令:

image
运行后,无上述错误,同时会生成一个report文件。其中会有一个xml格式的报告:

image

image
当然xml格式的报告不够直观,我们需要通过allure将它转成HTML格式的报告。通过cmd命令cd到report的根目录下,执行allure generate --clean report

image
回到根目录下,会生成一个allure-report的文件夹,在pycharm中打开文件夹,点击index.html运行

image
ok,到此为止。可以看到我们的精美的测试报告了

image

作者:努力学习的小白
链接:https://www.jianshu.com/p/9673b2aeb0d3
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
浙公网安备 33010602011771号