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

  1. allure包下载:https://github.com/allure-framework/allure2/releases
  2. 解压 -> 进入bin目录 -> 运行allure.bat,
  3. 把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需要在用例执行时添加参数:

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

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

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

结果:

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报告

测试步骤:

  1. 打开百度
  2. 搜索关键词
  3. 搜索结果截图,保存到报告中
  4. 退出浏览器

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测试框架(五):pytest + allure生成测试报告的更多相关文章

  1. pytest(11)-Allure生成测试报告(一)

    Allure是一个开源的测试报告生成框架,提供了测试报告定制化功能,相较于我们之前使用过pytest-html插件生成的html格式的测试报告,通过Allure生成的报告更加规范.清晰.美观. pyt ...

  2. 『德不孤』Pytest框架 — 1、Pytest测试框架介绍

    目录 1.什么是单元测试框架 2.单元测试框架主要做什么 3.单元测试框架和自动化测试框架有什么关系 4.Pytest测试框架说明 5.Pytest框架和Unittest框架区别 (1)Unittes ...

  3. python + pytest + allure生成测试报告

    pytest结合allure生成测试报告 环境搭建 要安装java环境,版本要是jdk1.8的,配置好java环境变量,不然输入allure命令会报错,JAVA_HOME环境,自行配置 安装allur ...

  4. pytest测试框架 -- 简介

    一.pytest测试框架简介: (1)pytest是python的第三方测试框架,是基于unittest的扩展框架,比unittest更简洁,更高效. (2)pytest框架可以兼容unittest用 ...

  5. Pytest测试框架(一):pytest安装及用例执行

    PyTest是基于Python的开源测试框架,语法简单易用,有大量的插件,功能非常多.自动检测测试用例,支持参数化,跳过特定用例,失败重试等功能. 安装 pip install -U pytest  ...

  6. Pytest测试框架(二):pytest 的setup/teardown方法

    PyTest支持xUnit style 结构, setup() 和 teardown() 方法用于初始化和清理测试环境,可以保证测试用例的独立性.pytest的setup/teardown方法包括:模 ...

  7. Pytest测试框架(三):pytest fixture 用法

    xUnit style 结构的 fixture用于初始化测试函数, pytest fixture是对传统的 xUnit 架构的setup/teardown功能的改进.pytest fixture为测试 ...

  8. pytest + allure 生成测试报告

    pytest测试样例规则:测试文件以test_开头(以_test结尾也可以)测试类以Test开头,并且不能带有 init 方法测试函数以test_开头断言使用基本的assert即可 ubuntu 安装 ...

  9. Pytest使用pytest-html和allure生成测试报告

    Pytest-html 1.安装命令pip3 install pytest-html,如下图: 执⾏后,会在当前⽬录下⽣成 ⼀个report.html的⽂件,打开后会展示详细的测试报告,执行该命令py ...

随机推荐

  1. MySQL-索引分类及使用索引

    1.什么是索引? 索引:存储引擎用于快速找到记录的一种数据结构,默认使用B-Tree索引.索引是存储引擎层中实现.简单理解为:排好序的快速查找数据结构 索引的目的:提高数据查询的效率,优化查询性能,就 ...

  2. 定位方式 及CSS高级技巧

    定位 background-position 背景位置 浮动,在一个浮字上面,我们的定位,在一个位上.CSS离不开定位,特别是后面的JS特效,天天和定位打交道. 为什么要使用定位? 元素的定位属性 元 ...

  3. Go-数据类型-字符串-string

    字符串 -- string 字符串使用双引号表示 " " 多行或原始字符串(反斜杠失去转义行为)使用 反引号 `` 字符使用单引号表示 ' ',表示单独的字母.中文.数字 字符串转 ...

  4. 微信小程序中使用text-indent实现首行缩进

    问题由来:在小程序中使用text标签包裹了一段话,要做一个首行缩进的效果,但是不起效果 . 解决方法:使用view标签代替text,使用text-indent:2em即可解决.

  5. 【面试专栏】ArrayList 非线程安全案例并提供三种解决方案

    1. 复现问题 import java.util.ArrayList; import java.util.List; import java.util.UUID; /** * 复现问题 * * @au ...

  6. double类型和int类型的区别

    引例: double a=19*3.3; System.out.print(a); 结果为62.9999996,不是62.7:这里不单纯是因为给的是double类型 (1) 62.7 和 62.699 ...

  7. elementUI的动态tabs页的使用,vue的动态组件的操作

    elementUI的动态tabs页的使用,vue的动态组件的操作 有时候我们需要用到动态的tab页,结合不同的页面内容来显示.这里是使用了elementUI的动态tabs页来实现的 <div c ...

  8. vue第五单元(v-if和v-show以及v-for的灵活应用 watch以及computed的区别 (常见效果) #课程目标

    第五单元(v-if和v-show以及v-for的灵活应用 watch以及computed的区别 (常见效果) #课程目标 精通 v-if v-else v-else-if 的使用 精通 v-show  ...

  9. Unity使用小剧场—创建的按钮On Click()只有MonoScript怎么办

    前言: 在游戏开发过程中遇到了一些小问题,以后都放到小剧场里,今天介绍怎么给按钮赋予方法并解决标题所述问题. 步骤: 1. 不管怎么说,先新建一个按钮 右键场景-[UI]-[Button] 这里会自动 ...

  10. CCNP第二天之复习CCNA

    1.静态路由的扩展配置: (1).环回接口: 在设备上用于测试TCP/IP协议栈能否正常使用.默认没有.需要手工创建   R1(config)#interface loopback 1         ...