Pytest测试框架(五):pytest + allure生成测试报告
系列文章目录
Pytest测试框架(一):pytest安装及用例执行
Pytest测试框架(二):pytest 的setup/teardown方法
Pytest测试框架(三):pytest fixture 用法
Pytest测试框架(四):pytest 参数化用例
Pytest测试框架(五):pytest + allure生成测试报告
Pytest插件开发
目录
- 系列文章目录
- 环境安装
- 安装allure
- 安装 allure-pytest插件
- 生成Allure报告
- 运行
- 查看测试报告
- 方法1
- 方法2
- allure特性—feature, storry, step
- allure特性—link, issue, testcase
- allure特性—severity
- allure.attach()
- pytest + selenium + allure报告
Allure 是一款轻量级、支持多语言的开源自动化测试报告生成框架,由Java语言开发,可以集成到 Jenkins。 pytest 测试框架支持Allure 报告生成。
pytest也可以生成junit格式的xml报告和HTML报告,命令如下:
pytest test_demo.py --junitxml=report.xml
pytest test_demo.py --html=report.html #需要安装插件:pip install pytest-html
Allure 报告更加灵活美观,本文介绍如何使用pytest 生成 allure测试报告
环境安装
安装allure
- allure包下载:https://github.com/allure-framework/allure2/releases
- 解压 -> 进入bin目录 -> 运行allure.bat,
- 把bin目录加入PATH环境变量
allure官网 : http://allure.qatools.ru/
allure文档 : https://docs.qameta.io/allure/#
安装 allure-pytest插件
pip install allure-pytest
生成Allure报告
运行
pytest [测试文件] -s -q --alluredir=./result #--alluredir用于指定存储测试结果的路径
查看测试报告
方式一:直接打开默认浏览器展示报告
allure serve ./result/
方式二:从结果生成报告
生成报告
allure generate ./result/ -o ./report/ --clean
(覆盖路径加–clean)打开报告
allure open -h 127.0.0.1 -p 8883 ./report/
实例代码:https://docs.qameta.io/allure/#_pytest
test_allure.py:
import pytestdef test_success():"""this test succeeds"""assert Truedef test_failure():"""this test fails"""assert Falsedef test_skip():"""this test is skipped"""pytest.skip('for a reason!')def test_broken():raise Exception('oops')
方法1
执行测试用例:
pytest test_allure.py --alluredir=./result/1
打开报告:
> allure serve ./result/1
Generating report to temp directory...
Report successfully generated to C:\Users\10287\AppData\Local\Temp\6968593833275403330\allure-report
Starting web server...
2020-10-25 20:59:42.368:INFO::main: Logging initialized @4873ms to org.eclipse.jetty.util.log.StdErrLog
Server started at <http://169.254.57.162:60084/>. Press <Ctrl+C> to exit
方法2
allure generate ./result/1 -o ./report/1/ --clean
allure open -h 127.0.0.1 -p 8883 ./report/1
浏览器访问地址 http://127.0.0.1:8883/ ,会显示跟上图一样的报告。
allure特性—feature, storry, step
可以在报告中添加用例描述信息,比如测试功能,子功能或场景,测试步骤以及测试附加信息:
- @allure.feature(‘功能名称’):相当于 testsuite
- @allure.story(’子功能名称‘):对应这个功能或者模块下的不同场景,相当于 testcase
- @allure.step(‘步骤’):测试过程中的每个步骤,放在具体逻辑方法中
- allure.step(‘步骤’) 只能以装饰器的形式放在类或者方法上面
- with allure.step:可以放在测试用例方法里面
- @allure.attach(‘具体文本信息’)
- 附加信息:数据,文本,图片,视频,网页
测试用例 test_feature_story_step.py:
import pytest
import allure@allure.feature("登录")
class TestLogin():@allure.story("登录成功")def test_login_success(self):print("登录成功")pass@allure.story("密码错误")def test_login_failure(self):with allure.step("输入用户名"):print("输入用户名")with allure.step("输入密码"):print("输入密码")print("点击登录")with allure.step("登录失败"):assert '1' == 1print("登录失败")pass@allure.story("用户名密码错误")def test_login_failure_a(self):print("用户名或者密码错误,登录失败")pass@allure.feature("注册")
class TestRegister():@allure.story("注册成功")def test_register_success(self):print("测试用例:注册成功")pass@allure.story("注册失败")def test_register_failure(self):with allure.step("输入用户名"):print("输入用户名")with allure.step("输入密码"):print("输入密码")with allure.step("再次输入密码"):print("再次输入密码")print("点击注册")with allure.step("注册失败"):assert 1 + 1 == 2print("注册失败")pass
用例执行、生成报告
pytest test_feature_story.py --alluredir=./result/2
allure generate ./result/2 -o ./report/2/ --clean
allure open -h 127.0.0.1 -p 8883 ./report/2
报告:
allure特性—link, issue, testcase
可以在测试报告中添加链接、bug地址、测试用例地址。
关联bug需要在用例执行时添加参数:
- –allure-link-pattern=issue:[bug地址]{}
- 例如:–allure-link-pattern=issue:http://www.bugfree.com/issue/{}
test_allure_link_issue.py:
import allure@allure.link("http://www.baidu.com", name="baidu link")
def test_with_link():pass@allure.issue("140","this is a issue")
def test_with_issue_link():passTEST_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:http://www.bugfree.com/issue/{} --alluredir=./result/3
allure serve ./result/3
报告:
点击 this is a issue
,页面会跳转到bug页面:http://www.bugfree.com/issue/140
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 "登录成功" //只运行登录成功子模块
通过 allure.severity按重要性级别来标记,有5种级别:
Blocker级别:阻塞
Critical级别:严重
Normal级别:正常
Minor级别:不太重要
Trivial级别:不重要
test_allure_severity.py:
import allure
import pytestdef 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
结果:
allure.attach()
可以在报告中附加文本、图片以及html网页,用来补充测试步骤或测试结果,比如错误截图或者关键步骤的截图。
test_allure_attach.py:
import allure
import pytestdef 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", name="图片", attachment_tye=allure.attachment_type.JPG)
用例执行:
pytest test_allure_attach.py --alluredir=./result/5
allure serve ./result/5
结果:
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
结果:
--THE END--
文章标题:Pytest测试框架(五):pytest + allure生成测试报告
本文作者:hiyo
本文链接:https://blog.csdn.net/u010698107/article/details/111416173
欢迎关注公众号:「测试开发小记」及时接收最新技术文章!
Pytest测试框架(五):pytest + allure生成测试报告相关推荐
- pytest测试框架(四)---使用pytest-html生成测试报告
一.pytest-html 1.pytest-html模块用于生成测试报告. 2.使用方法: pytest 用例.py --html=/路径/report.html 3.上面的方法,css是独立的,如 ...
- pytest测试框架(七)---pytest与jenkins集成(pytest+jenkins+allure)
目录 一.pytest注册命令行选项 1.addoption 2.pytestconfig.getoption 3.示例 二.Jenkins上显示allure测试报告 1.安装插件 2.配置Allur ...
- pytest测试框架(三)---使用allure描述测试用例
目录 一.allure的准备 二.使用allure描述测试用例 三.遇到的问题 一.allure的准备 1.allure命令行工具的准备:描述测试用例前,需要先下载allure命令行工具,解压到本地, ...
- pytest测试框架(五)---使用xfail将用例标记为失败
一.简介 当因为一个确切的原因,我们知道这个用例会执行失败,比如用例所覆盖的功能还未实现,或者这个功能存在阻塞性的已知Bug时,就可以使用xfail将其标记起来. 二.xfail的使用方法 1.@py ...
- pytest测试框架系列 - pytest 断言assert使用
前言 测试用例的预期结果是用例不可缺少的一部分,那么断言就是自动化测试不可缺少的一步,一个没有断言的用例,自动化测试的就没有意义了.那什么是断言呢? 简单来讲就是实际结果和期望结果去对比,符合预期那就 ...
- Python编程必不可少的pytest测试框架
进行编程测试重要的是为了更高效的完成功能的实现. pytest是基于unittest实现的第三方测试框架,比 unittest 更加的简洁.高效,并且可以完美兼容 unittest 的测试代码,无需对 ...
- pytest和allure生成测试报告
文章目录 pytest简介 pytest和unittest区别 pytest使用自带的python-html 生成测试报告 由于自带生成测试报告的东西不是界面过于简单和不太美观可以使用allure 使 ...
- Pytest测试框架(二):pytest 的setup/teardown方法
系列文章目录 Pytest测试框架(一):pytest安装及用例执行 Pytest测试框架(二):pytest 的setup/teardown方法 Pytest测试框架(三):pytest fixtu ...
- Pytest 测试框架——数据驱动
引言 前面已经和大家介绍过 Unittest 测试框架的数据驱动框架 DDT,以及其实现原理.今天和大家分享的是 Pytest 测试框架的数据驱动,Pytest 测试框架的数据驱动是由 pytest ...
最新文章
- win 安装Polygon
- Python绘图 二维、三维
- OpenCV立体校准stereo calib的实例(附完整代码)
- mysql创建表格时候数据格式_mysql建表语句和数据类型
- 第五章 Response(JavaTM Servlet 规范3.1 )
- oracle 查询不同编号的时间最小记录_投稿 | ORACLE amp; PostgreSql 利用伪列删除完全重复的两行...
- webpack 编译stylus_webpack 文档更新日志(9.12-9.29)
- php计算一段时间工作日,PHP计算8小时工作日的一半
- java 实例的生命周期_[Java教程]Vue实例生命周期
- mysql 连接错误The server time zone value ‘?????????‘
- SqlServer 2008R2修改表结构提示“不允许保存更改”解决方案
- 完整的蓝屏错误代码大全详解
- 美化传奇NPC对话框添加图片显示实列
- vue项目之微信分享
- AJAX聊天室实现原理 JQuery+PHP
- 1.2 win10下Docker 和 Docker Compose的安装
- Flutter2.5新特性 中文版
- Python 中文(大写)数字转阿拉伯数字
- 2013年最后的收成:avalon1.0正式发布
- 数据库decimal对应java什么类型_数据库字段类型和Java的对应关系