1、安装allure:
a、下载&解压allure安装包:
https://github.com/allure-framework/allure2/releases/tag/2.14.0
b、配环境变量:
/bin到path路径下
2、pycharm:安装allure-pytest
File->Settings->Project->Project Intepreter->+allure-pytest
3、可能出现的问题:
3.1、AttributeError: module ‘allure‘ has no attribute ‘severity_level‘
解决方案:
1)、pip uninstall pytest-allure-adaptor
2)、pip install allure-pytest
3.2、ModuleNotFoundError: No module named ‘allure.constants‘; ‘allure‘ is not a package
解决方案:
1)卸载掉通过pycharm安装的allure,直接通过下载allure安装包的形式进行安装
4、安装完成,查看allure版本:
1)、allure --version
5、生成allure测试报告
第一种:通过命令行方式生成报告
1)、生成报告的格式:pytest+测试用例所在的路径 + -- +测试报告所在的路径
pytest /Users/.../test_res.py --alluredir /Users/.../report/xml
2)、生成测试报告到临时目录的格式:allure serve +测试报告所在的路径
allure serve /Users/.../report/xml
3)、生成测试报告到指定目录的格式:allure serve +测试报告所在的路径
allure generate --clean /Users/.../report/xml -o /Users/.../report/html
第二种:通过main方式生成报告
Mac:相对路径
1)、pytest.main(["--alluredir", "./report/xml","--allure-severities=blocker", "./"])
2)、subprocess.getstatusoutput("allure serve ./report/xml")
3)、subprocess.getstatusoutput("allure generate --clean ./report/xml -o ./report/html")
Windows: 相对路径
1)、pytest.main(["--alluredir", ".\\report\\xml", ".\\"])
2)、subprocess.getstatusoutput("allure serve .\\report\\xml")
3)、subprocess.getstatusoutput("allure generate --clean .\\report\\xml -o .\\report\\html")
6、查看allure常用参数:
Windows:pytest --help | findstr "allure"
Mac:pytest --help | grep "allure"
allure常用参数说明:
--allure-severities=SEVERITIES_SET
--allure-features=FEATURES_SET
--allure-stories=STORIES_SET
--alluredir=DIR Generate Allure report in the specified directory (may
--clean-alluredir Clean alluredir folder if it exists
--allure-no-capture Do not attach pytest captured logging/stdout/stderr to
1)、有已存在的报告就先清空->在指定目录生成测试报告(不加载 logging/stdout/stderr 文件到报告):
pytest --alluredir ./test_pytest//report/xml --clean-alluredir --allure-no-capture ./test_pytest/test_login.py
2)、根据优先级执行测试用例:
pytest --alluredir ./test_pytest//report/xml --allure-severities=blocker ./test_pytest/test_login.py
3)、根据features执行测试用例:
pytest --alluredir ./test_pytest//report/xml --allure-features=登录 ./test_pytest/test_login.py
4)、根据story执行测试用例:
pytest --alluredir ./test_pytest//report/xml --allure-stories=登录测试场景一 ./test_pytest/test_login.py
9、allure常用装饰器:
@allure.epic() 史诗
@allure.feature 模块
@allure.story 用户故事
@allure.severity 用例级别
@allure.title 用例标题
@allure.description 用例描述
@allure.step 用例操作步骤
@allure.testcase 用例
@allure.link 用例链接
@allure.issue 问题
@allure.attach 文本或者截图附件
allure测试用例:
@allure.feature("登录")
class Testlogin2:
@allure.story("登录测试场景一")
@allure.severity("blocker")
@allure.title("用例名称:正确的账号")
@allure.description("用例描述:判断输入的账号是否和预期的账号一致")
@allure.testcase("https://baidu.com")
@allure.link("https://www.cnblogs.com/poloyy/p/12726946.html")
@allure.issue("https://hbtown.worktile.com")
@pytest.mark.parametrize("user_name_001", ["zhangsan111", "lisi111", "sandra"])
@pytest.mark.usefixtures("user_name_001")
def test_user_001(self, user_name_001):
with allure.step("先输入账号名称"):
logging.info("#" * 100 + "先输入账号名称")
time.sleep(random.randint(1, 10))
logging.info("#" * 100 + "等待XX秒")
allure.attach("我是文字说明")
assert user_name_001 == "sandra"
@allure.story("登录测试场景二")
@allure.severity("blocker")
@allure.title("用例名称:错误的账号")
@allure.description("用例描述:判断输入的账号是否和预期的账号一致")
@allure.testcase("https://baidu.com")
@allure.issue("https://hbtown.worktile.com/mission/projects/5f4f1744503e515888e7fed4/tasks/60f7cc751292bf76e0a2bc14")
@pytest.mark.parametrize("user_name_002", ["zhangsan222", "lisi222", "sandra222"])
@pytest.mark.usefixtures("user_name_002")
def test_user_002(self, user_name_002):
with allure.step("先输入账号名称"):
logging.info("#" * 100 + "先输入账号名称")
time.sleep(random.randint(1, 10))
logging.info("#" * 100 + "等待XX秒")
allure.attach.file(r'/Users/liuzhe/PycharmProjects/demo102/test_pytest/东关国际.jpg', '我是附件截图',
attachment_type=allure.attachment_type.JPG)
assert user_name_002 == "sandra"
allure测试报告

网友评论