Pytest测试框架(五):pytest + allure生成测试报告 (2)

test_allure_link_issue.py:

import allure @allure.link("http://www.baidu.com",) def test_with_link(): pass @allure.issue("140","this is a issue") def test_with_issue_link(): pass TEST_CASE_LINK = 'https://github.com' @allure.testcase(TEST_CASE_LINK, 'Test case title') def test_with_testcase_link(): pass

用例执行:

pytest test_allure_link_issue.py --allure-link-pattern=issue:{} --alluredir=./result/3 allure serve ./result/3

报告:

Pytest测试框架(五):pytest + allure生成测试报告

点击 this is a issue,页面会跳转到bug页面:

allure特性—severity

有时候在上线前,由于时间关系,我们只需要把重要模块测试一遍,在这样的场景下我们怎么实现呢?主要有三种方法:

可以使用pytest.mark来标记用例,Pytest测试框架(一):pytest安装及用例执行 介绍了这种方法。@pytest.mark.webtest # 添加标签 @pytest.mark.sec pytest -m "webtest and not sec"

通过 allure.feature, allure.story来实现pytest test_feature_story_step.py --allure-features "登录" //只运行登录模块 pytest test_feature_story_step.py --allure-stories "登录成功" //只运行登录成功子模块 ​```sss

3. 通过 allure.severity按重要性级别来标记,有5种级别: - Blocker级别:阻塞 - Critical级别:严重 - Normal级别:正常 - Minor级别:不太重要 - Trivial级别:不重要 test_allure_severity.py: ​```python import allure import pytest def test_with_no_severity_label(): pass @allure.severity(allure.severity_level.TRIVIAL) def test_with_trivial_severity(): pass @allure.severity(allure.severity_level.NORMAL) def test_with_normal_severity(): pass @allure.severity(allure.severity_level.NORMAL) class TestclassWithNormalSeverity(object): def test_inside_the_normalseverity_test_class(self): pass @allure.severity(allure.severity_level.CRITICAL) def test_inside_the_normal_severity_test_class_with_overriding_critical_severity(self): pass

用例执行:

pytest test_allure_severity.py --alluredir=./result/4 --allure-severities normal,critical allure serve ./result/4

结果:

Pytest测试框架(五):pytest + allure生成测试报告

allure.attach()

可以在报告中附加文本、图片以及html网页,用来补充测试步骤或测试结果,比如错误截图或者关键步骤的截图。

test_allure_attach.py:

import allure import pytest def test_attach_text(): allure.attach("纯文本", attachment_type=allure.attachment_type.TEXT) def test_attach_html(): allure.attach("<body>这是一段htmlbody块</body>", "html页面", attachment_type=allure.attachment_type.HTML) def test_attach_photo(): allure.attach.file("test.jpg",, attachment_tye=allure.attachment_type.JPG)

用例执行:

pytest test_allure_attach.py --alluredir=./result/5 allure serve ./result/5

结果:

Pytest测试框架(五):pytest + allure生成测试报告

pytest+selenium+allure报告

测试步骤:

打开百度

搜索关键词

搜索结果截图,保存到报告中

退出浏览器

test_allure_baidu.py:

import allure import pytest from selenium import webdriver import time @allure.testcase("http://www.github.com") @allure.feature("百度搜索") @pytest.mark.parametrize('test_data1', ['allure', 'pytest', 'unittest']) def test_steps_demo(test_data1): with allure.step("打开百度网页"): driver = webdriver.Chrome("D:/testing_tools/chromedriver85/chromedriver.exe") driver.get("http://www.baidu.com") with allure.step("搜索关键词"): driver.find_element_by_id("kw").send_keys(test_data1) time.sleep(2) driver.find_element_by_id("su").click() time.sleep(2) with allure.step("保存图片"): driver.save_screenshot("./result/b.png") allure.attach.file("./result/b.png", attachment_type=allure.attachment_type.PNG) allure.attach('<head></head><body>首页</body>', 'Attach with HTML type', allure.attachment_type.HTML) with allure.step("退出浏览器"): driver.quit()

用例执行:

pytest test_allure_baidu.py --alluredir=./result/6 allure serve ./result/6

结果:

Pytest测试框架(五):pytest + allure生成测试报告

--THE END--

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wspyss.html