[pytest] 配置

这里写目录标题

  • Pytest
    • Init
    • Run
    • Report
      • --junitxml={report}.xml
        • [1. testsuite](#1. testsuite)
          • [1.1 在测试套件级别添加属性节点 record_testsuite_property](#1.1 在测试套件级别添加属性节点 record_testsuite_property)
        • [2. testcase](#2. testcase)
          • [2.1 记录测试的其他信息 record_property](#2.1 记录测试的其他信息 record_property)
          • [2.2 向testcase元素添加额外的xml属性 record_xml_attribute](#2.2 向testcase元素添加额外的xml属性 record_xml_attribute)
    • Hooks
    • [other plugin 好玩的](#other plugin 好玩的)

Pytest

Init

Run

  1. 引进@pytest.mark.parametrize中ids导致编码乱码
python 复制代码
def pytest_collection_modifyitems(items):
    for item in items:
        item.name = item.name.encode('utf-8').decode('unicode-escape')
        item._nodeid = item._nodeid.encode('utf-8').decode('unicode-escape')

Report

--junitxml={report}.xml

https://docs.pytest.org/en/stable/reference/reference.html

pytest v6.0+ 默认 xunit2 不支持 testcase添加属性

建议设置

  • pytest.ini中配置junit_family=xunit1
  • pytest -o junit_family=xunit1
text 复制代码
  junit_suite_name (string):
                        Test suite name for JUnit report
  junit_logging (string):
                        Write captured log messages to JUnit report: one of no|log|system-out|system-err|out-err|all
  junit_log_passing_tests (bool):
                        Capture log information for passing tests to JUnit report:
  junit_duration_report (string):
                        Duration time to report: one of total|call
  junit_family (string):
                        Emit XML for schema: one of legacy|xunit1|xunit2
bash 复制代码
pytest -h
--junit-xml=path      create junit-xml style report file at given path. # 报告地址
--junit-prefix=str    prepend prefix to classnames in junit-xml output # classname前缀

> pytest demo --junit-xml=./report.xml --junit-prefix=xxx
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<testsuites>
    <testsuite name="pytest" errors="0" failures="0" skipped="0" tests="2" time="0.113"
               timestamp="2025-03-10T14:53:08.040765+08:00" hostname="Ding-Perlis-MP1Y70F1">
        <testcase classname="xxx.set_classname" name="set_name" file="test_case.py" line="49" time="0.006"/>
        <testcase classname="xxx.set_classname" name="set_name" file="test_case.py" line="49" time="0.001"/>
    </testsuite>
</testsuites>
1. testsuite
1.1 在测试套件级别添加属性节点 record_testsuite_property

支持xunit2

python 复制代码
import pytest


@pytest.fixture(scope="session", autouse=True)
def log_global_env_facts(record_testsuite_property):
    record_testsuite_property("ARCH", "PPC")
    record_testsuite_property("STORAGE_TYPE", "CEPH")


class TestMe:
    def test_foo(self):
        assert True

output

xml 复制代码
<testsuite errors="0" failures="0" name="pytest" skipped="0" tests="1" time="0.006">
    <properties>
        <property name="ARCH" value="PPC"/>
        <property name="STORAGE_TYPE" value="CEPH"/>
    </properties>
    <testcase classname="test_me.TestMe" file="test_me.py" line="16" name="test_foo" time="0.000243663787842"/>
</testsuite>
2. testcase
2.1 记录测试的其他信息 record_property

请注意,使用此功能将中断对最新JUnitXML架构的架构验证。当与某些CI服务器一起使用时,这可能是一个问题

  • 方法一 test_case.py
python 复制代码
def test_function(record_property):
    record_property("example_key", 1)
    assert True
  • 方法二 contest.py
python 复制代码
# content of conftest.py

def pytest_collection_modifyitems(session, config, items):
    for item in items:
        for marker in item.iter_markers(name="test_id"):
            test_id = marker.args[0]
            item.user_properties.append(("test_id", test_id))


# content of test_function.py
import pytest


@pytest.mark.test_id(1501)
def test_function():
    assert True

output

xml 复制代码
<testcase classname="test_function" file="test_function.py" line="0" name="test_function" time="0.0009">
    <properties>
        <property name="example_key" value="1"/>
    </properties>
</testcase>
<testcase classname="test_function" file="test_function.py" line="0" name="test_function" time="0.0009">
<properties>
    <property name="test_id" value="1501"/>
</properties>
</testcase>
2.2 向testcase元素添加额外的xml属性 record_xml_attribute

record_xml_attribute 是一个实验性的特性,它的接口在未来的版本中可能会被更强大和通用的东西所取代。然而,功能本身将保持不变
请注意,使用此功能将中断对最新JUnitXML架构的架构验证。当与某些CI服务器一起使用时,这可能是一个问题

  • 方法一 test_case.py
python 复制代码
@pytest.mark.parametrize("case", case_data)
def test_case(case, record_xml_attribute):
    record_xml_attribute('classname', 'set_classname')  # 重写 value
    record_xml_attribute('name', 'set_name')  # 重写 value
    record_xml_attribute('index', '123')  # 新增 key, value
    print("hello world")
    assert True
  • 方法二 contest.py
python 复制代码
# edit to contest.py
@pytest.fixture(autouse=True)
def record_index(record_xml_attribute):
    record_xml_attribute('index', '123')  # 新增 key, value
  • output
xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<testsuites>
    <testsuite name="pytest" errors="0" failures="0" skipped="0" tests="2" time="0.113"
               timestamp="2025-03-10T14:53:08.040765+08:00" hostname="Ding-Perlis-MP1Y70F1">
        <testcase classname="set_classname" name="set_name" file="test_case.py" line="49" index="123" time="0.006">
            <system-out>
                hello world
            </system-out>
        </testcase>
        <testcase classname="set_classname" name="set_name" file="test_case.py" line="49" index="123" time="0.001"/>
    </testsuite>
</testsuites>

Hooks

other plugin 好玩的

相关推荐
2601_967979129 小时前
初学者必知的Python中优雅的用法
python·枚举·初学者·优雅用法·字典解析
AI创界者11 小时前
Python 进阶实战(五十八章):配置热更新、优雅停机排空与高并发压测方案
人工智能·python·aigc
2601_9679873715 小时前
python网络工程师自动化运维课程
python·正则表达式·网络工程师·模块·自动化运维
吴凡wudianmei15 小时前
给 LLM 套输出契约:校验失败怎么把错误回灌给模型
python·llm·agent
祺曦16 小时前
复现连续手语识别项目
python·手语识别·复现项目·corrnet
江润舟16 小时前
万物|炼器:从零手搓工业级旋转目标检测网络·卷7 —— OBB检测头与DFL解码(三)
人工智能·python·深度学习
AI创界者17 小时前
Python 进阶实战(五十六):KV Cache 驱逐闭环反馈与 Prefill / Decode 分离协同调度
人工智能·python·aigc
2601_9679801117 小时前
Python - 变量作用域
python·闭包·变量作用域·nonlocal·嵌套函数
山楂树の18 小时前
Python 模块、包、面向对象编程
python
青少儿编程课堂19 小时前
蓝桥杯真题解析:三维偏序的CDQ分治(全国青少年信息素养大赛与电子学会编程考级)
c++·python·算法·bfs·信息学竞赛