蟑螂恶霸的博客 蟑螂恶霸的博客
首页
  • Web自动化
  • 自动化测试框架
  • 接口自动化
  • 测试面试题
  • 技术文档
  • GitHub技巧
  • 博客搭建
  • Vue
  • JavaScript
  • Nginx
  • 自动化测试
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

蟑螂恶霸

一个爱折腾的程序员,什么都想试试!
首页
  • Web自动化
  • 自动化测试框架
  • 接口自动化
  • 测试面试题
  • 技术文档
  • GitHub技巧
  • 博客搭建
  • Vue
  • JavaScript
  • Nginx
  • 自动化测试
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • web自动化

  • 自动化测试框架

  • 接口自动化测试

  • 测试面试题

  • Pytest

    • 快速入门和基础讲解
    • assert断言详细使用
    • setup和teardown的详细使用
    • fixture的详细使用
    • 测试用例执行后的几种状态
    • conftest.py的详细讲解
    • skip、skipif跳过用例
    • 使用自定义标记mark
    • 参数化@pytest.mark.parametrize
    • fixture 传参数 request的详细使用
    • 失败重跑插件pytest-rerunfailures的详细使用
    • 测试结果生成HTML报告插件之pytest-html的详细使用
    • 重复执行用例插件之pytest-repeat的详细使用
    • 配置文件pytest.ini的详细使用
    • 多重校验插件之pytest-assume的详细使用
    • 分布式测试插件之pytest-xdist的详细使用
    • pytest-xdist分布式测试的原理和流程
    • 超美测试报告插件之allure-pytest的基础使用
    • 我们需要掌握的allure特性
    • allure的特性,@allure.step()、allure.attach的详细使用
      • 前言
      • @allure.step
        • 示例代码
        • 测试用例在allure上的显示
        • 知识点
      • allure.attach(挺有用的)
        • 参数列表
        • allure.attachment_type提供了哪些附件类型?
        • 语法二: allure.attach.file(source, name, attachment_type, extension)
        • 其中一个测试用例的代码栗子
        • 运行之后看结果
    • allure的特性,@allure.description()、@allure.title()的详细使用
    • allure的特性,@allure.link()、@allure.issue()、@allure.testcase()的详细使用
    • allure 打标记之 @allure.epic()、@allure.feature()、@allure.story() 的详细使用
    • allure 环境准备
    • allure.severity 标记用例级别
    • 清空 allure 历史报告记录
    • allure 命令行参数
    • 参数化 parametrize + @allure.title() 动态生成标题
    • 详解 allure.dynamic 动态生成功能
    • 使用 pytest-xdist 分布式插件,如何保证 scope=session 的 fixture 在多进程运行情况下仍然能只运行一次
  • 自动化测试
  • Pytest
蟑螂恶霸
2022-07-22
目录

allure的特性,@allure.step()、allure.attach的详细使用

# 前言

allure除了支持pytest自带的特性之外(fixture、parametrize、xfail、skip),自己本身也有强大的特性可以在pytest中使用

# @allure.step

  • allure报告最重要的一点是,它允许对每个测试用例进行非常详细的步骤说明
  • 通过 @allure.step() 装饰器,可以让测试用例在allure报告中显示更详细的测试过程

# 示例代码

import allure


@allure.step("第一步")
def passing_step():
    pass

@allure.step("第二步")
def step_with_nested_steps():
    nested_step()


@allure.step("第三步")
def nested_step():
    nested_step_with_arguments(1, 'abc')


@allure.step("第四步{0},{arg2}")
def nested_step_with_arguments(arg1, arg2):
    pass

@allure.step("第五步")
def test_with_nested_steps():
    passing_step()
    step_with_nested_steps()
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

# 测试用例在allure上的显示

# 知识点

  • step() 只有一个参数,就是title,你传什么,在allure上就显示什么
  • 可以像python字符串一样,支持位置参数和关键字参数 {0},{arg2},可看第四步那里,如果函数的参数没有匹配成功就会报错哦
  • step() 的使用场景,给我感觉就是,当方法之间嵌套会比较有用,否则的话只会显示一个步骤,类似下面图

# allure.attach(挺有用的)

**作用:**allure报告还支持显示许多不同类型的附件,可以补充测试结果;自己想输出啥就输出啥,挺好的

语法: allure.attach(body, name, attachment_type, extension)

# 参数列表

  • body:要显示的内容(附件)
  • name:附件名字
  • attachment_type:附件类型,是 allure.attachment_type 里面的其中一种
  • extension:附件的扩展名(比较少用)

# allure.attachment_type提供了哪些附件类型?

# 语法二: allure.attach.file(source, name, attachment_type, extension)

source:文件路径,相当于传一个文件

其他参数和上面的一致

# 其中一个测试用例的代码栗子

import allure
import pytest


@pytest.fixture
def attach_file_in_module_scope_fixture_with_finalizer(request):
    allure.attach('在fixture前置操作里面添加一个附件txt', 'fixture前置附件', allure.attachment_type.TEXT)

    def finalizer_module_scope_fixture():
        allure.attach('在fixture后置操作里面添加一个附件txt', 'fixture后置附件',
                      allure.attachment_type.TEXT)

    request.addfinalizer(finalizer_module_scope_fixture)

def test_with_attacments_in_fixture_and_finalizer(attach_file_in_module_scope_fixture_with_finalizer):
    pass

def test_multiple_attachments():
    allure.attach('<head></head><body> 一个HTML页面 </body>', 'Attach with HTML type', allure.attachment_type.HTML)
    allure.attach.file('./reports.html', attachment_type=allure.attachment_type.HTML)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# 运行之后看结果

这是一个txt附件

这是一个用了 allure.attach() 来插入一段自己写的HTML和 allure.attach.file() 来导入一个已存在的HTML文件(pytest-html报告)

本文转自 https://www.cnblogs.com/poloyy/p/12716659.html (opens new window),如有侵权,请联系删除。

上次更新: 2022/10/15, 15:19:25
我们需要掌握的allure特性
allure的特性,@allure.description()、@allure.title()的详细使用

← 我们需要掌握的allure特性 allure的特性,@allure.description()、@allure.title()的详细使用→

最近更新
01
实现定时任务数据库配置
06-09
02
SQL Server连接
02-22
03
RSA加密工具类
02-22
更多文章>
Theme by Vdoing | Copyright © 2022-2023 蟑螂恶霸 | Blog
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式