pytest + allure生成测试报告(pytest生成allure报告命令)
ccwgpt 2024-11-02 11:02 33 浏览 0 评论
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 pytest
def test_success():
"""this test succeeds"""
assert True
def test_failure():
"""this test fails"""
assert False
def 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' == 1
print("登录失败")
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 == 2
print("注册失败")
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():
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: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 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
结果:
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", 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--
本文作者:hiyo
本文链接:https://www.cnblogs.com/hiyong/p/14163298.html
相关推荐
- MFC、Qt、WPF?该用哪个?(mfc和wpf区别)
-
MFC、Qt和WPF都是流行的框架和工具,用于开发图形用户界面(GUI)应用程序。选择哪个框架取决于你的具体需求和偏好。MFC(MicrosoftFoundationClass)是微软提供的框架,...
- 一款WPF开发的通讯调试神器(支持Modbus RTU、MQTT调试)
-
我们致力于探索、分享和推荐最新的实用技术栈、开源项目、框架和实用工具。每天都有新鲜的开源资讯等待你的发现!项目介绍Wu.CommTool是一个基于C#、WPF、Prism、MaterialDesign...
- 关于面试资深C#、WPF开发工程师的面试流程和问题
-
一、开场(2-3分钟)1.欢迎应聘者,简单介绍公司和面试流程。2.询问应聘者是否对公司或岗位有初步的问题。二、项目经验与技术应用(10-20分钟)1.让应聘者详细介绍几个他参与过的C#、...
- C# WPF MVVM模式Prism框架下事件发布与订阅
-
01—前言处理同模块不同窗体之间的通信和不同模块之间不同窗体的通信,Prism提供了一种事件机制,可以在应用程序中低耦合的模块之间进行通信,该机制基于事件聚合器服务,允许发布者和订阅者之间通过事件进行...
- WPF 机械类组件动画制作流程简述(wps上怎么画机械结构简图)
-
WPF机械类组件动画制作流程简述独立观察员2025年3月4日一、创建组件创建组件用户控件,将组件的各部分“零件”(图片)拼装在一起,形成组件的默认状态:二、给运动部分加上Rend...
- C#上位机WinForm和WPF选哪个?工控老油条的"血泪史"
-
作为一个从互联网卷进工控坑的"跨界难民",在这会摸鱼的时间咱就扯一下上位机开发选框架这档子破事。当年我抱着WPF的酷炫动画一头扎进车间,结果被产线老师傅一句"你这花里胡哨的玩意...
- 【一文扫盲】WPF、Winform、Electron有什么区别?
-
近年来,随着软件开发的不断发展,开发人员面临着选择适合他们项目的各种框架和工具的挑战。在桌面应用程序开发领域,WPF、Winform和Electron是三个备受关注的技术。本文将介绍这三者的区别,帮助...
- 一个开源、免费、强大且美观的WPF控件库
-
我们致力于探索、分享和推荐最新的实用技术栈、开源项目、框架和实用工具。每天都有新鲜的开源资讯等待你的发现!项目介绍HandyControl是一套基于WPF(WindowsPresentationF...
- WPF 根据系统主题自动切换浅色与深色模式
-
WPF根据系统主题自动切换浅色与深色模式控件名:Resources作者:WPFDevelopersOrg-驚鏵原文链接[1]:https://github.com/WPFDevelopers...
- WPF与WinForm的本质区别(wpf与maui)
-
在Windows应用程序开发中,WinForm和WPF是两种主要的技术框架。它们各自有不同的设计理念、渲染机制和开发模式。本文将详细探讨WPF与WinForm的本质区别,并通过示例进行说明。渲染机制W...
- Win10/Win11效率神器再进化:微软发布PowerToys 0.90.0版本
-
IT之家4月1日消息,微软今天(4月1日)更新PowerToys,在最新发布的0.90.0版本中,修复多个BUG之外,引入多项功能更新,为Windows10、Windows...
- 一款非常漂亮的WPF管理系统(wpf架构及特性)
-
我们致力于探索、分享和推荐最新的实用技术栈、开源项目、框架和实用工具。每天都有新鲜的开源资讯等待你的发现!WPFManager项目介绍该项目是一款WPF开发的管理系统,数据库采用的MSSqlserv...
- WPF 实现描点导航(wpf按钮的点击事件)
-
WPF实现描点导航控件名:NavScrollPanel作者:WPFDevelopersOrg-驚鏵原文链接[1]:https://github.com/WPFDevelopersOrg/WPF...
- 微软更新基于Win11的Validation OS 2504:增强 .NET与WPF
-
IT之家5月1日消息,科技媒体NeoWin今天(5月1日)发布博文,报道称微软公司更新基于Windows11的ValidationOS,增强支持.NET和WPF,并优...
- WPF的技术架构与优势(wpf的前景)
-
WindowsPresentationFoundation(WPF)是一个现代化的用户界面框架,专为构建Windows应用程序而设计。它通过分层的技术架构和丰富的功能集,提供了全面的应用程...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- MVC框架 (46)
- spring框架 (46)
- 框架图 (58)
- bootstrap框架 (43)
- flask框架 (53)
- quartz框架 (51)
- abp框架 (47)
- jpa框架 (47)
- laravel框架 (46)
- express框架 (43)
- springmvc框架 (49)
- 分布式事务框架 (65)
- scrapy框架 (52)
- java框架spring (43)
- grpc框架 (55)
- orm框架有哪些 (43)
- ppt框架 (48)
- 内联框架 (52)
- winform框架 (46)
- gui框架 (44)
- cad怎么画框架 (58)
- ps怎么画框架 (47)
- ssm框架实现登录注册 (49)
- oracle字符串长度 (48)
- oracle提交事务 (47)