3.3.1 命令行选项
您可以通过使用通用帮助选项获得有关ini样式配置文件中的命令行选项和值的帮助:
pytest -h 这将显示由已安装的插件注册的命令行和配置文件设置。
3.3.2 配置文件格式
许多 pytest 设置可以在配置文件中设置,按照惯例,该配置文件位于存储库的根目录中。 pytest.ini
文件优先于其他文件,即使是空的。
[pytest]
minversion = 6.0
addopts = -ra -q
testpaths =
tests
integration
其他配置文件了解下:
[tool.pytest.ini_options]
minversion = "6.0"
addopts = "-ra -q"
testpaths = [
"tests",
"integration", ]
[pytest]
minversion = 6.0
addopts = -ra -q
testpaths =
tests
integration
[tool:pytest]
minversion = 6.0
addopts = -ra -q
testpaths =
tests
integration
3.3.3 – 5 不深入
本页面包含了对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 )
assert exc_info.type is ValueError
- 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 以特殊方式处理一些全局变量。
collect_ignore = ["setup.py"]
collect_ignore_glob = ["*_ignore.py"]
pytest_plugins = "myapp.testsupport.myplugin"
pytest_plugins = ("myapp.testsupport.tools", "myapp.testsupport.regression")
import pytest
pytestmark = pytest.mark.webtest
pytestmark = [pytest.mark.integration, pytest.mark.slow]
3.4.5 配置选项
1. addopts
[pytest]
addopts = --maxfail=2 -rf
实际上是这样运行的: pytest --maxfail=2 -rf test_hello.py
2. pytest的所有参数配置
[pytest]
addopts = --maxfail=2 -rf
console_output_style = classic
empty_parameter_set_mark = xfail
faulthandler_timeout=5
filterwarnings =
error
ignore::DeprecationWarning
junit_duration_report = call
junit_family = xunit2
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 =
clean_db
xfail_strict = True
3.4.6 – 10 【不深入】
Original: https://blog.csdn.net/weixin_45451320/article/details/124183915
Author: 阿_焦
Title: pytest学习总结3.3 – pytest.ini、API解读
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/772871/
转载文章受原作者版权保护。转载请注明原作者出处!