目录
Allure安装
Allure是一个独立的报告插件,生成美观易读的报告,目前支持语言:Java, PHP, Ruby, Python, Scala, C#
Allure安装
安装pytest的插件包pytest-allure-adaptor: pip3 install pytest-allure-adaptor
Allure帮助文档
https://docs.qameta.io/allure/#_about
生成Allure报告
命令行参数:pytest --alluredir report # 在执行命令目录生成report文件夹,文件夹下包含xml文件
执行时报错pluggy.manager.PluginValidationError: unknown hook ‘pytest_namespace’ in plu
解决办法:
pytest-allure-adaptor与allure-pytest兼容问题
卸载pytest-allure-adaptor,安装allure-pytest
allure出现乱码
将pycharm的语言改为GBK就行了
pycharm–settings–Editor–File encodeing–改为GBK
allure’ 不是内部或外部命令,也不是可运行的程序
解决方案:
官网下载路径
https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/
下载zip文件,解压,找到bin文件目录,添加到环境变量path中
allure --version查看是否安装成功(可能需要重启cmd和pycharm)
示例
import os
import pytest
# def test_a():
# print("-----a")
class Test_ABC:
def test_a(self):
print("-----a")
assert 1
if __name__ == '__main__':
#运行该目录下所有的test测试用例
#pytest.main(["-s","pytest_001.py"])
#生成测试报告json
pytest.main(["--alluredir","report/result","pytest_001.py"])
#将测试报告转化为html
split= 'allure'+ ' generate' + ' ./report/result'+ ' -o'+' ./report/html'+' --clean'
os.system(split)
打开report/html中的html文件
Allure语言使用
1.@allure.step()
allure报告最重要的一点是,它允许对每个测试用例进行非常详细的步骤说明
通过 @allure.step() 装饰器,可以让测试用例在allure报告中显示更详细的测试过程
import os
import pytest
import allure
# def test_a():
# print("-----a")
class Test_ABC:
@allure.step("这是测试步骤1")
def test_a(self):
print("-----a")
self.test_003("aa", "bb")
assert 1
@allure.step("这是测试步骤2")
def test_002(self):
print("---002---")
assert 0
@allure.step("这是测试步骤3{ar1},{ar2}")
def test_003(self,ar1,ar2):
print("---003---")
assert 1
if __name__ == '__main__':
#运行该目录下所有的test测试用例
#pytest.main(["-s","pytest_001.py"])
#生成测试报告json
pytest.main(["--alluredir","report/result","pytest_001.py"])
#将测试报告转化为html
#split= 'allure'+ ' generate' + ' ./report/result'+ ' -o'+' ./report/html'+' --clean'
split= 'allure generate ./report/result -o ./report/html --clean'
os.system(split)
或者通过with allure.step的方式说明:
import os
import pytest
import allure
# def test_a():
# print("-----a")
class Test_ABC:
@allure.step("这是测试步骤1")
def test_a(self):
print("-----a")
with allure.step("测试步骤3"):
self.test_003("aa", "bb")
assert 1
@allure.step("这是测试步骤2")
def test_002(self):
print("---002---")
assert 0
def test_003(self,ar1,ar2):
print("---003---")
assert 1
if __name__ == '__main__':
#运行该目录下所有的test测试用例
#pytest.main(["-s","pytest_001.py"])
#生成测试报告json
pytest.main(["--alluredir","report/result","pytest_001.py"])
#将测试报告转化为html
#split= 'allure'+ ' generate' + ' ./report/result'+ ' -o'+' ./report/html'+' --clean'
split= 'allure generate ./report/result -o ./report/html --clean'
os.system(split)
2.allure.attach(挺有用的)
作用:allure报告还支持显示许多不同类型的附件,可以补充测试结果;自己想输出啥就输出啥,挺好的
语法: allure.attach(body, name, attachment_type, extension)
参数列表
body:要显示的内容(附件)
name:附件名字
attachment_type:附件类型,是 allure.attachment_type 里面的其中一种
extension:附件的扩展名(比较少用)
allure.attachment_type提供了哪些附件类型?
TEXT = ("text/plain", "txt")
CSV = ("text/csv", "csv")
TSV = ("text/tab-separated-values", "tsv")
URI_LIST = ("text/uri-list", "uri")
HTML = ("text/html", "html")
XML = ("application/xml", "xml")
JSON = ("application/json", "json")
YAML = ("application/yaml", "yaml")
PCAP = ("application/vnd.tcpdump.pcap", "pcap")
PNG = ("image/png", "png")
JPG = ("image/jpg", "jpg")
SVG = ("image/svg-xml", "svg")
GIF = ("image/gif", "gif")
BMP = ("image/bmp", "bmp")
TIFF = ("image/tiff", "tiff")
MP4 = ("video/mp4", "mp4")
OGG = ("video/ogg", "ogg")
WEBM = ("video/webm", "webm")
PDF = ("application/pdf", "pdf")
语法二: allure.attach.file(source, name, attachment_type, extension)
source:文件路径,相当于传一个文件
其他参数和上面的一致
使用方式一:
def test_003(self,ar1=None,ar2=None):
print("---003---",ar1,ar2)
allure.attach("这是一个附件说明","附件名称",allure.attachment_type.TEXT)
assert 1
使用方式2:
def test_003(self,ar1=None,ar2=None):
print("---003---",ar1,ar2)
#allure.attach("这是一个附件说明","附件名称",allure.attachment_type.TEXT)
allure.attach.file("文件目录","文件名称",allure.attachment_type.TEXT)
assert 1
3.@allure.severity
标记用例等级
BLOCKER = ‘blocker’
CRITICAL = ‘critical’
NORMAL = ‘normal’
MINOR = ‘minor’
TRIVIAL = ‘trivial’
import os
import pytest
import allure
# def test_a():
# print("-----a")
class Test_ABC:
@allure.severity(allure.severity_level.BLOCKER)
@allure.step("这是测试步骤1")
def test_a(self):
print("-----a")
with allure.step("测试步骤3"):
self.test_003("aa", "bb")
assert 1
@allure.severity(allure.severity_level.NORMAL)
@allure.step("这是测试步骤2")
def test_002(self):
print("---002---")
assert 0
@allure.severity(allure.severity_level.CRITICAL)
def test_003(self,ar1=None,ar2=None):
print("---003---",ar1,ar2)
allure.attach("这是一个附件说明","附件名称",allure.attachment_type.TEXT)
#allure.attach.file("文件目录","文件名称",allure.attachment_type.TEXT)
assert 1
if __name__ == '__main__':
#运行该目录下所有的test测试用例
#pytest.main(["-s","pytest_001.py"])
#生成测试报告json
pytest.main(["--alluredir","report/result","--allure-severities","blocker","pytest_001.py"])
#将测试报告转化为html
#split= 'allure'+ ' generate' + ' ./report/result'+ ' -o'+' ./report/html'+' --clean'
split= 'allure generate ./report/result -o ./report/html --clean'
os.system(split)
命令行参数 allure-severities
也可以根据优先级选择需要运行的测试用例
只运行 severity=blocker、critical 的测试用例
pytest test_severity.py -sq --alluredir=./allure --allure-severities=blocker,critical
写法二
pytest test_severity.py -sq --alluredir=./allure --allure-severities blocker,critical