python-pytest-Allure2测试报告生成
Allure:
Allure框架是一种灵活的轻量级多语言测试报告工具,它以简洁的web报告形式显示已测试的内容。
安装环境(win10):
- 安装JDK1.8+环境:
Allure需要java8+,JDK 1.8+ 环境,所以要提前配置好java环境。
官方下载地址:https://www.oracle.com/java/technologies/javase-jdk8-downloads.html,1.8后的jdk会自动添加环境变量 - 安装Allure:
https://github.com/allure-framework/allure2/releases 进行下载,下载Links中Download的压缩包之后,解压到本地 - 安装allure命令:
pip install allure-pytest
安装完成会提示:Installing collected packages: allure-python-commons, allure-pytest
Successfully installed allure-pytest-2.8.13 allure-python-commons-2.8.13
allure用例描述:
- allure.epic("xxx") ----------------> 参数:敏捷测试中的概念 史诗,可以理解为项目级别的描述
- allure.feature("xxx") ------------> 参数:模块描述,功能点描述
- allure.stroy("xxx") ---------------> 参数:用例描述,用例故事
- allure.title("xxx") -----------------> 参数:用例重命名的标题,显示报告中,不重命名则显示函数/方法名
- allure.step("xxx") ----------------> 参数:测试步骤的描述
- allure.description("xxx") -------> 参数:测试用例描述
- allure.severity("xxx") -----------> 参数:用例等级(blocker、critical、normal、minor、trivial)
- allure.attachment("xxx") ------> 参数:报告中添加的附件
- allure.testcase("xxx") ----------> 参数:功能测试用例链接地址
- allure.issue("xxx") --------------> 参数:缺陷链接地址
- allure.link("xxx") -----------------> 参数:定义一个链接,显示在报告中
添加environment:
通过创建environment.properties或者environment.xml文件,并把文件存放到报告依赖文件的同级目录下,就是--alluredir 后面跟的目录
# environment.xml文件如下:Browser Chrome
# environment.properties文件内容 Browser = Chrome python.Version = 3.7.2
添加categories:
分类:测试结果的分类,默认两类缺陷
1. Product defects 产品缺陷 (测试结果failed)
2. Test defects 测试缺陷 (测试结果:error/broken)
我们可以自定义缺陷,将categories.json 文件添加到 报告文件存放的目录
# 官方例子 categories.json [ { "name": "Ignored tests", "matchedStatuses": ["skipped"] }, { "name": "Infrastructure problems", "matchedStatuses": ["broken", "failed"], "messageRegex": ".*bye-bye.*" }, { "name": "Outdated tests", "matchedStatuses": ["broken"], "traceRegex": ".*FileNotFoundException.*" }, { "name": "Product defects", "matchedStatuses": ["failed"] }, { "name": "Test defects", "matchedStatuses": ["broken"] } ]
官方字段解释:
name: (mandatory) category name matchedStatuses:(optional) list of suitable test statuses. Default ["failed", "broken", "passed", "skipped", "unknown"] messageRegex: (optional) regex pattern to check test error message. Default ".*" traceRegex: (optional) regex pattern to check stack trace. Default ".*"
pytest 执行并生成报告的过程:
pytest --alluredir ./report/allure_raw
执行完成后,在当前目录下,report目录会生成一个allure_raw的原始文件,这个只是测试报告的原始文件,不能打开成html的报告。
allure serve report/allure_raw
启动服务,它会自动给个端口,直接用默认浏览器打开了,也可以手动复制地址在其他浏览器中打开。
指定执行用例:
pytest --alluredir ./report/allure --allure-epics="epic的描述信息" pytest --alluredir ./report/allure --allure-features="feature描述" pytest --alluredir ./report/allure --allure-stories="story描述"