前面介绍了Pytest-html的插件,今天主要介绍Pytest生成Allure的测试报告。Allure的官方地址为:https://docs.qameta.io/allure/。在Allure的的框架里面可以指定测试报告的Title以及其他的信息,可以依据自己的需求来自定义测试报告。首先需要安装插件allure-pytest,安装的命令为:
pip3 install allure-pytest
如下图显示安装该插件的信息:
下来在地址https://github.com/allure-framework/allure2/releases/tag/2.7.0下载Allure,下载成功后,配置它的环境变量,如mac中,它的环境变量设置为:
sudo vim .bash_profile,配置如下:
Allure="/Applications/devOps/allure-2.7.0/bin"
PATH=${Allure}:${PATH}
配置环境变量成功后,输入allure显示如下图信息说明Allure配置成功:
下来结合具体的案例来说明这部分的应用,简单写一个测试的函数,来小测试该函数的测试点,设计到的源码为:
#!/usr/bin/python3#coding:utf-8import pytestimport time as tdef add(a,b):try:return a+bexcept Exception as e:return e.args[0]@pytest.mark.parametrize('a,b,result',[
(1,1,2), (1.0,1.0,2.0), (1, 1.0, 2.0), (1,0,1), ('','',''), ('hi ','wuya','hi wuya'), (0, '', "unsupported operand type(s) for +: 'int' and 'str'"), (1,'hi',"unsupported operand type(s) for +: 'int' and 'str'"), (1.0,'wuya',"unsupported operand type(s) for +: 'float' and 'str'"),])def test_add(a,b,result):
t.sleep(1)assert add(a,b)==result
在该模块的当前路径执行如下命令执行测试用例:
pytest -v test_function.py --alluredir report/result/
执行如下命令生成测试报告
allure generate report/result/ -o report/html --clean
执行如下命令让结果信息展示在浏览器中
allure open -h 127.0.0.1 -p 8088 ./report/html
感谢您的阅读和关注,后续会持续的更新,谢谢!