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
盘
![](https://upload-images.jianshu.io/upload_images/6635063-bc3711acd6bfdaa9.jpg)
image
3.2. 配置allure
的环境变量
![](https://upload-images.jianshu.io/upload_images/6635063-161b6820361874c2.jpg)
image
![](https://upload-images.jianshu.io/upload_images/6635063-895bcdc1704323d6.jpg)
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
![](https://upload-images.jianshu.io/upload_images/6635063-34b7ec190d7a91af.jpg)
image
输入命令pytest -s --alluredir=report
,会遇到以下这个错误:
![](https://upload-images.jianshu.io/upload_images/6635063-8c8ea6c6eca54d4f.jpg)
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))
![](https://upload-images.jianshu.io/upload_images/6635063-20a864730227cb08.jpg)
image
修改之后,再次运行pytest -s --alluredir=report
命令:
![](https://upload-images.jianshu.io/upload_images/6635063-4dad96667972742d.jpg)
image
运行后,无上述错误,同时会生成一个report
文件。其中会有一个xml
格式的报告:
![](https://upload-images.jianshu.io/upload_images/6635063-89ea5f38a1c9aa00.jpg)
image
![](https://upload-images.jianshu.io/upload_images/6635063-360dbbed825d43f8.jpg)
image
当然xml格式的报告不够直观,我们需要通过allure
将它转成HTML
格式的报告。通过cmd
命令cd
到report
的根目录下,执行allure generate --clean report
![](https://upload-images.jianshu.io/upload_images/6635063-117bf1b0c6b7d480.jpg)
image
回到根目录下,会生成一个allure-report
的文件夹,在pycharm
中打开文件夹,点击index.html
运行
![](https://upload-images.jianshu.io/upload_images/6635063-b1e8f5842a4e8219.jpg)
image
ok,到此为止。可以看到我们的精美的测试报告了
![](https://upload-images.jianshu.io/upload_images/6635063-055d924e28723389.jpg)
image
![](https://upload-images.jianshu.io/upload_images/6635063-32ea385df130420e.jpg)
作者:努力学习的小白
链接:https://www.jianshu.com/p/9673b2aeb0d3
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。