PyTest 弃用:'junit_family 默认值将更改为 'xunit2'

PyTest deprecation: 'junit_family default value will change to 'xunit2'

我在 circleci.

的管道收到弃用警告

留言.

/home/circleci/evobench/env/lib/python3.7/site-packages/_pytest/junitxml.py:436: PytestDeprecationWarning: The 'junit_family' default value will change to 'xunit2' in pytest 6.0.

命令

- run:
    name: Tests
    command: |
      . env/bin/activate
      mkdir test-reports
      python -m pytest --junitxml=test-reports/junit.xml

我应该如何修改命令以使用 xunit? 是否可以使用消息中提到的默认工具? 我的意思是没有 specyfing xunit 或 junit。

这是完整的 pipeline

在您的 pytest.ini 文件中添加以下行:

junit_family=legacy

如果你想保留--junitxml选项的默认行为。或者您可以接受新版本 xunit2 但不显式定义 junit_family 变量。

本质上,警告的意思是您在

中提供了 --junitxml 选项
run           
  name: Tests

部分未指定 junit_family 变量。您需要开始明确定义它以删除警告或接受新的默认值。

This thread goes into more details about where to find the .ini file for pytest.

运行你这样命令。

使用 xunit2

python -m pytest -o junit_family=xunit2 --junitxml=test-reports/junit.xml

与 xunit1

python -m pytest -o junit_family=xunit1 --junitxml=test-reports/junit.xml

python -m pytest -o junit_family=legacy --junitxml=test-reports/junit.xml

This here 详细描述变化:

The default value of junit_family option will change to xunit2 in pytest 6.0, given that this is the version supported by default in modern tools that manipulate this type of file.

In order to smooth the transition, pytest will issue a warning in case the --junitxml option is given in the command line but junit_family is not explicitly configured in pytest.ini:

PytestDeprecationWarning: The `junit_family` default value will change to 'xunit2' in pytest 6.0.   Add `junit_family=legacy` to your

pytest.ini file to silence this warning and make your suite compatible.

In order to silence this warning, users just need to configure the junit_family option explicitly:

[pytest]
junit_family=legacy

关于从 xunit1 移动到 xunit2 的官方 statement/documentation 阅读:docs.pytest.org

此外,如果您的项目包含 pytest.ini 文件,您可以设置 junit_family 直接从文件中使用,如:

# pytest.ini
[pytest]
minversion = 6.0
junit_family=xunit2
junit_suite_name = Pytest Tests
addopts = -ra -q -v -s --junitxml=path/to/pytest_results/pytest.xml

其他答案几乎涵盖了指定 junit 系列的方法,在 pytest.ini 中或在命令行中使用 ini 选项覆盖。

对于具体的 xml 文件,值得查看 xunit1 和 xunit2 之间的区别。快速抽查差异,我发现这些差异显示在下图中的以下测试模块...

# test_stub.py

import sys
import pytest

def test_pass():
    assert True

def test_fail():
    assert False


if __name__ == "__main__":
    sys.exit(pytest.main([__file__] + sys.argv[1:]))

Pytest 运行 在三种不同的配置下(与图像中的垂直顺序相匹配)

# Default execution
pytest test_stub.py --junit-xml=out.xml 
pytest test_stub.py --junit-xml=out_xunit2.xml -o junit_family=xunit2

# Junit prefix execution
pytest test_stub.py --junit-prefix=FOOOP --junit-xml=out_prefix.xml
pytest test_stub.py --junit-prefix=FOOOP --junit-xml=out_prefix_xunit2.xml -o junit_family=xunit2

# Junit suite execution
pytest -o junit_suite_name=SUITE test_stub.py --junit-xml=out_suite.xml
pytest -o junit_suite_name=SUITE test_stub.py --junit-xml=out_suite_xunit2.xml -o junit_family=xunit2

所有差异都突出了一个事实,即 xunit2 省略了先前在 xunit1 中出现的 fileline 属性。其他差异仅仅是时间戳差异。 junit_suite_namejunit-prefix 的行为都和以前一样。