Pytest 系列(26)- 清空 allure 历史报告记录

一、背景

  • pytest 运行 测试用例生成 allure 报告时,当测试用例名称修改后重新运行,会保留历史运行记录
  • 又或者分开运行两个测试用例文件,但是 allure 报告生成目录是同一个,那么 allure 报告会同时显示两个文件的测试用例运行情况
  • 咱们来看看这种情况

1.1 目录结构

下面两个栗子都是这个目录结构

img

1.2 修改名称的例子

1.2.1 test_1.py 的代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-

def test_1():
    print("test_1 文件的测试用例1")


def test_2():
    print("test_1 文件的测试用例2")

1.2.2 运行命令

进入该目录下,cmd 运行

pytet test_1.py --alluredir=./allure

1.2.3 allure 报告

img

只有两条用例

1.2.4 修改后的 test_1.py 的代码

def test_11():
    print("test_1 文件的测试用例1")


def test_22():
    print("test_1 文件的测试用例2")

1.2.5 再次运行命令,查看 allure 报告

img

1.3 分开运行测试用例文件的例子

1.3.1 test_2.py 的代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-

def test_1():
    print("test_1 文件的测试用例1")


def test_2():
    print("test_1 文件的测试用例2")

1.3.2 分开运行 test_1 和 test_2 两个测试用例文件

# 先运行第一个
pytet test_1.py --alluredir=./allure

# 再运行第二个,此时应该希望 allure 报告只有 test_2.py 的测试用例
pytet test_2.py --alluredir=./allure

1.3.3 查看 allure 报告

img

​ 出现了 test_1.py 的测试用例,这不是想要的结果

二、--clean-alluredir 参数

2.1 前言

  • pytest 提供了 --clean-alluredir 参数可以清空 allure 报告生成的目录
  • 可以看看 pytest 的说明文档
pytest -h

img

2.2 将上面的例子重新运行

# 先运行第一个
pytet test_1.py --alluredir=./allure

# 再运行第二个,此时应该希望 allure 报告只有 test_2.py 的测试用例
pytet test_2.py --alluredir=./allure --clean-alluredir

2.3 运行结果

img

原文地址:https://www.cnblogs.com/dongye95/p/14047712.html