pytest学习总结3.3 - pytest.ini、API解读

3.3 配置

3.3.1 命令行选项

您可以通过使用通用帮助选项获得有关ini样式配置文件中的命令行选项和值的帮助:
pytest -h 这将显示由已安装的插件注册的命令行和配置文件设置。

3.3.2 配置文件格式

许多 pytest 设置可以在配置文件中设置,按照惯例,该配置文件位于存储库的根目录中。pytest.ini文件优先于其他文件,即使是空的。

# pytest.ini
[pytest]
minversion = 6.0
addopts = -ra -q
testpaths =
    tests
    integration

其他配置文件了解下:

# pyproject.toml
[tool.pytest.ini_options]
minversion = "6.0"
addopts = "-ra -q"
testpaths = [
    "tests",
    "integration", ]
# tox.ini
[pytest]
minversion = 6.0
addopts = -ra -q
testpaths =
    tests
    integration
# setup.cfg
[tool:pytest]
minversion = 6.0
addopts = -ra -q
testpaths =
    tests
    integration

3.3.3 - 5 不深入

3.4 API解读

本页面包含了对pytest的API的完整引用

3.4.1 方法 - Functions:

  • pytest.approx
from pytest import approx
if 0.1 + 0.2 == approx(0.3):
    print("通过1")
if {'a': 0.1 + 0.2, 'b': 0.2 + 0.4} == approx({'a': 0.3, 'b': 0.6}):
    print("通过2")
  • pytest.fail(reason=‘失败的理由’)
  • pytest.skip(reason=‘跳过的理由’)
  • pytest.importorskip :如果无法导入模块,则跳过当前测试
  • pytest.xfail : 必须使用给定的原因 xfail 正在执行的测试或设置功能,此函数应仅在测试期间调用(设置、调用或拆卸)。
  • pytest.exit :退出测试过程
  • pytest.main : 执行进程中的测试
  • pytest.param
import pytest
@pytest.mark.parametrize(
    "test_input,expected",
    [
        ("3+5", 8),
        pytest.param("6*9", 42, marks=pytest.mark.xfail),
    ],
)
def test_eval(test_input, expected):
    assert eval(test_input) == expected
  • pytest.raises
import pytest
value = 15
with pytest.raises(ValueError) as exc_info:
    if value > 10:
        raise ValueError("value must be <= 10")
    assert exc_info.type is ValueError # this will not execute
  • pytest.deprecated_call
  • pytest.register_assert_rewrite
  • pytest.warns
  • pytest.freeze_includes

3.4.2 标记 - Marks

  • pytest.mark.filterwarnings
  • pytest.mark.parametrize
  • pytest.mark.skip
  • pytest.mark.skipif
  • pytest.mark.usefixtures
  • pytest.mark.xfail
  • Custom marks

3.4.3 内置的固定装置 - fixture

  • @pytest.fixture
  • config.cache
  • capsys
  • capsysbinary
  • capfd
  • capfdbinary
  • doctest_namespace
  • request
  • pytestconfig
  • record_property
  • record_testsuite_property
  • caplog
  • monkeypatch
  • pytester
  • testdir
  • recwarn
  • tmp_path
  • tmp_path_factory
  • tmpdir
  • tmpdir_factory

3.4.4 内置钩子函数 - Hooks

  • Bootstrapping hooks
  • Initialization hooks
  • Collection hooks
  • Test running (runtest) hooks
  • Reporting hooks
  • Debugging/Interaction hooks

3.4.5 全局变量

当在测试模块或 conftest.py 文件中定义时,pytest 以特殊方式处理一些全局变量。

  1. collect_ignore:conftest.py中设置 ,会忽略文件setup.py
    可以在 conftest.py 文件中声明以排除测试目录或模块。 需要是路径列表
collect_ignore = ["setup.py"]
  1. collect_ignore_glob,conftest.py中设置,会忽略匹配的文件,
    可以在 conftest.py 文件中声明以排除带有 Unix shell 样式通配符的测试目录或模块:
collect_ignore_glob = ["*_ignore.py"]
  1. 可以在测试模块和 conftest.py 文件中在全局级别声明以注册其他插件:
pytest_plugins = "myapp.testsupport.myplugin"
pytest_plugins = ("myapp.testsupport.tools", "myapp.testsupport.regression")
  1. 可以在测试模块的全局级别声明,以将一个或多个标记应用于所有测试函数和方法。 可单个标记或标记列表
import pytest
pytestmark = pytest.mark.webtest
pytestmark = [pytest.mark.integration, pytest.mark.slow]

3.4.5 配置选项

1. addopts

# content of pytest.ini
[pytest]
addopts = --maxfail=2 -rf # 2次失败后退出,报告失败信息

实际上是这样运行的:pytest --maxfail=2 -rf test_hello.py

2. pytest的所有参数配置

# content of pytest.ini
[pytest]
addopts = --maxfail=2 -rf # 2次失败后退出,报告失败信息
console_output_style = classic
empty_parameter_set_mark = xfail 
faulthandler_timeout=5  # 如果测试运行时间超过 X 秒(包括夹具设置和拆除),则转储所有线程的回溯。
filterwarnings =  # 设置对匹配的警告应采取的过滤器和操作列表。 默认情况下,期间发出的所有警告测试会话将在测试会话结束时显示在摘要中
    error
    ignore::DeprecationWarning
junit_duration_report = call  # 配置如何将持续时间记录到 JUnit XML 报告中,报告的持续时间仅包括通话时间,不包括设置和拆卸时间
junit_family = xunit2 # 产生xunit 2.0 风格的输出,应该更兼容最新的Jenkins 版本。这是默认设置。
junit_logging = system-out # 日志相关
junit_log_passing_tests = False
junit_suite_name = my_suite
log_auto_indent = False
log_cli = True
log_cli_date_format = %Y-%m-%d %H:%M:%S
log_cli_format = %(asctime)s %(levelname)s %(message)s
log_cli_level = INFO
log_date_format = %Y-%m-%d %H:%M:%S
log_file = logs/pytest-logs.txt
log_file_date_format = %Y-%m-%d %H:%M:%S
log_file_format = %(asctime)s %(levelname)s %(message)s
log_file_level = INFO
log_format = %(asctime)s %(levelname)s %(message)s
log_level = INFO

addopts = --strict-markers
markers =
    slow
    serial

norecursedirs = .svn _build tmp*

python_classes = *Suite
python_files = test_*.py check_*.py example_*.py
python_files =
    test_*.py
    check_*.py
    example_*.py
python_functions = *_test
pythonpath = src1 src2

required_plugins = pytest-django>=3.0.0,<4.0.0 pytest-html pytest-xdist>=1.0.0
testpaths = testing doc
usefixtures =  # 将应用于所有测试功能的夹具列表,这在语义上与应用 @pytest.mark.usefixtures 标记所有测试功能。
    clean_db
xfail_strict = True

3.4.6 - 10 【不深入】

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿_焦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值