1、allure下载安装(注意:allure的安装需要Java1.8+的环境)
(1)下载链接:https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/2.13.2/
(2)下载.zip文件解压
(3)解压后配置系统环境变量
(4)在cmd.exe中 运行 allure --version,看到 allure 版本,即表示 allure 命令行工具安装成功
注意:如当前Pycharm是打开的状态,安装完成后则需要重启下Pycharm才能在此正常使用allure
2、安装pytest模块:pip install pytest
3、安装allure-pytest模块:pip install allure-pytest
(1)allure-pytest是python的第三方库,用于连接pytest和allure,使它们可以配合使用;
(2)allure-pytest基于pytest的原始执行结果生成适用于allure的json格式结果,该json格式结果可以用于后续适用allure生成html结果
4、在pycharm下编写案例,如:
import pytest
import allure
class Test_all():
@allure.step(title="allure通过注解方式完成内容的展示,setp表示测试步骤1...")
def test_setup(self):
print("我就是打酱油的setup")
@allure.step(title="run就是一个正常的方法.")
def test_run(self):
allure.attach("自定义描述1", "描述内容,自定义")
print("我要运行")
assert True
def test_skip(self):
print("我要跳过")
@allure.severity(allure.severity_level.BLOCKER) #严重级别
@allure.testcase("http://www.baidu.com/", "测试用例的地址")
@allure.issue("http://music.migu.cn/v3/music/player/audio", "点击可跳转到bug地址")
def test_error(self):
with allure.attach("自定义描述1", "我需要让他进行错误"):
print("我错误了")
assert False
(1)当通过pytest执行用例时,pytest会自动递归遍历执行路径下所有的目录,根据pytest中默认用例的识别的规则,自动收集测试用例。所有在使用pytest编写测试用例之前,需要了解一下pytest收集用例时默认的用例识别规则:
a、用例文件:所有文件名为 test_ 开头 或者 _test 开头的文件会被识别为用例文件;
b、用例类:测试文件中每个Test开头的类就是一个测试用例类;
c、测试用例:测试类中每个test开头的方法就是一条测试用例,测试文件中每个test开头的函数也是一条测试用例;
备注:上述默认的用例查找规则,可在pytest的配置文件进行修改;另外pytest兼容unittest,以unittest的用例编写规范写的用例,pytest都能够识别出来。
(2)allure装饰器描述
4、生成测试报告:在pycharm中打开terminal
切到对应case的文件夹下输入命令:
pytest -s --alluredir=report
或后续每次用例执行之前先清空allure的报告记录,可以加个参数 --clean-alluredir
pytest -s --alluredir=report --clean-alluredir
执行完后会生成一个report文件,会有一个json格式的报告
5、json格式的报告不够直观,需要通过allure将它转成HTML格式的报告。通过cmd命令cd到report的根目录下,执行:allure generate report
或后续可以加个参数 “–clean”: 清空测试报告目录,再生成新的测试报告,不然测试报告不会刷新
allure generate report --clean
6、执行allure generate report(allure generate report --clean)后,回到根目录下,会生成一个allure-report的文件夹,在pycharm中打开文件夹,点击index.html运行
注意:在allure-report目录下index.html文件是不能直接打开的,直接打开出现页面都是loading的情况,这是因为直接allure报告用浏览器是打不开的,需要用allure命令渲染之后打开才能展示效果。
allure使用了两种方式来渲染页面。分别是 allure open 和 allure serve。前者用于在本地渲染和查看结果,后者用于在本地渲染后对外展示结果。如切换到python工程文件根目录下运行命令:
allure open .\allure-report\
#或
allure serve .\report\
即可自动打开浏览器展示渲染好的结果。