2021-01-08

博客园Logo
首页
新闻
博问
专区
闪存
班级
代码改变世界
搜索
注册
登录
返回主页
测试开发小记
博客园
首页
新随笔
联系
订阅
管理
Pytest测试框架(五):pytest + 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 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(“这是一段htmlbody块”, “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://www.cnblogs.com/hiyong/p/14163298.html
欢迎关注公众号:「测试开发小记」及时接收最新技术文章!

标签: 自动化测试, 测试框架, pytest
好文要顶 关注我 收藏该文
测试开发小记
关注 - 0
粉丝 - 3
+加关注
00
« 上一篇: Pytest测试框架(四):pytest 参数化用例
posted @ 2021-01-08 07:07 测试开发小记 阅读(24) 评论(0) 编辑 收藏
刷新评论刷新页面返回顶部
登录后才能发表评论,立即 登录 或 注册, 访问 网站首页
【推荐】News: 大型组态、工控、仿真、CADGIS 50万行VC++源码免费下载
【推荐】有你助力,更好为你——博客园用户消费观调查,附带小惊喜!
【推荐】AWS携手博客园为开发者送福利,注册立享12个月免费套餐
【推荐】第一个NoSQL数据库,在大规模和一致性之间找到了平衡
【推荐】七牛云新老用户同享 1 分钱抢 CDN 1TB流量大礼包!
【推荐】了不起的开发者,挡不住的华为,园子里的品牌专区
【推荐】未知数的距离,毫秒间的传递,声网与你实时互动

相关博文:
· pytest生成allure报告
· pytest文档29-allure-pytest(最新最全,保证能搞成功!)
· pytest-2:allure 的安装、使用
· Allure+pytest生成测试报告
· Pytest+Allure安装过程问题小记
» 更多推荐…

最新 IT 新闻:
· 2025年中国大陆芯片将达2230亿美元 但自给率仍不到20%?
· 法拉第未来任命新CFO!贾跃亭激动发声
· 瑞幸咖啡“宫斗大戏”背后:陆正耀还想当王
· Git服务器配置错误导致日产汽车源码在网上泄露
· “996”“007”加班文化再引热议 谁来保护职场“打工人”权益?
» 更多新闻…
公告
昵称: 测试开发小记
园龄: 1个月
粉丝: 3
关注: 0
+加关注
< 2021年1月 >
日 一 二 三 四 五 六
27 28 29 30 31 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 1 2 3 4 5 6
搜索

我的标签
自动化测试(14)
Web自动化测试(8)
selenium(8)
pytest(5)
测试框架(5)
元素定位(4)
正则表达式(1)
版本管理(1)
App自动化测试(1)
Python(1)
更多
随笔档案
2021年1月(7)
2020年12月(9)
最新评论

  1. Re:Python正则表达式
    @小小咸鱼YwY 多谢提醒,修饰符只用过忽略大小写的,后续更新进去,,,…
    –测试开发小记
  2. Re:Python正则表达式
    @测试开发小记 你可以百度一下re模块修饰符…这个很重要的…
    –小小咸鱼YwY
  3. Re:Python正则表达式
    @小小咸鱼YwY 哪里?…
    –测试开发小记
  4. Re:Python正则表达式
    你少了,修饰符

–小小咸鱼YwY
阅读排行榜

  1. Python正则表达式(206)
  2. Web自动化测试:xpath & CSS Selector定位(131)
  3. Selenium执行JavaScript脚本(113)
  4. Android ADB原理及常用命令(104)
  5. Pytest测试框架(三):pytest fixture 用法(77)
    评论排行榜
  6. Python正则表达式(4)
    推荐排行榜
  7. Python正则表达式(1)
  8. Android ADB原理及常用命令(1)
    Copyright © 2021 测试开发小记
    Powered by .NET 5.0 on Kubernetes
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值