当前位置 : 主页 > 编程语言 > python >

pytest配置文件 -- pytest.ini

来源:互联网 收集:自由互联 发布时间:2022-06-15
pytest 相关技术汇总目录 官方文档介绍了四种配置文件,每种文件有各自的用处。 pytest.ini:主配置文件,最常用,优先匹配配置项 tox.ini :可以理解为pytest.ini的另一种写法,二者选择

pytest 相关技术汇总目录

 

官方文档介绍了四种配置文件,每种文件有各自的用处。

  • pytest.ini:主配置文件,最常用,优先匹配配置项
  • tox.ini :可以理解为pytest.ini的另一种写法,二者选择使用其一即可
  • setup.cfg
  • conftest.py:本地的插件库,主要用于存放fixture,其中的hook函数和fixture将作用于该文件所在的目录以及所有子目录

重点介绍pytest.ini和conftest.py

pytest --help # 查看可配置项

pytest配置文件 -- pytest.ini_pythonpytest配置文件 -- pytest.ini_测试用例_02

[pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg file found:

markers (linelist): markers for test functions
empty_parameter_set_mark (string):
default marker for empty parametersets
norecursedirs (args): directory patterns to avoid for recursion
testpaths (args): directories to search for tests when no files or directories are given in the command line.
usefixtures (args): list of default fixtures to be used with this project
python_files (args): glob-style file patterns for Python test module discovery
python_classes (args):
prefixes or glob names for Python test class discovery
python_functions (args):
prefixes or glob names for Python test function and method discovery
disable_test_id_escaping_and_forfeit_all_rights_to_community_support (bool):
disable string escape non-ascii characters, might cause unwanted side effects(use at your own
risk)
console_output_style (string):
console output: "classic", or with additional progress information ("progress" (percentage) |
"count").
xfail_strict (bool): default for the strict parameter of xfail markers when not given explicitly (default: False)
enable_assertion_pass_hook (bool):
Enables the pytest_assertion_pass hook.Make sure to delete any previously generated pyc cache
files.
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
doctest_optionflags (args):
option flags for doctests
doctest_encoding (string):
encoding used for doctest files
cache_dir (string): cache directory path.
filterwarnings (linelist):
Each line specifies a pattern for warnings.filterwarnings. Processed after -W/--pythonwarnings.
log_print (bool): default value for --no-print-logs
log_level (string): default value for --log-level
log_format (string): default value for --log-format
log_date_format (string):
default value for --log-date-format
log_cli (bool): enable log display during test run (also known as "live logging").
log_cli_level (string):
default value for --log-cli-level
log_cli_format (string):
default value for --log-cli-format
log_cli_date_format (string):
default value for --log-cli-date-format
log_file (string): default value for --log-file
log_file_level (string):
default value for --log-file-level
log_file_format (string):
default value for --log-file-format
log_file_date_format (string):
default value for --log-file-date-format
log_auto_indent (string):
default value for --log-auto-indent
faulthandler_timeout (string):
Dump the traceback of all threads if a test takes more than TIMEOUT seconds to finish. Not
available on Windows.
addopts (args): extra command line options
minversion (string): minimally required pytest version
rsyncdirs (pathlist): list of (relative) paths to be rsynced for remote distributed testing.
rsyncignore (pathlist):
list of (relative) glob-style paths to be ignored for rsyncing.
looponfailroots (pathlist):
directories to check for changes

View Code

一、pytest.ini

存放位置:配置文件一般都放在项目根目录,别乱发放

配置名字:固定为pytest.ini

[pytest]
xfail_strict = True
addopts = -v -rpEf
testpaths = ./dir01/dir01_test.py
markers =
weibo: this is weibo page
toutiao: toutiao
xinlang: xinlang

对于上面的配置文件,只需要执行:pytest  即代表没有改配置文件时的 pytest -v -rpEf dir01::dir01_test.py 指令

1.addopts

pytest执行时的默认命令行参数,可选参数见pytest用法。配置后执行时直接pytest即可,相当于后面默认加了命令行参数

如果pytest.ini配置的addopts和命令行的参数不一致,那么最终是以命令行的为准,如 pytest -q -rp  那么会以q简单模式输出而不是v模式,结果只显示passed的,不会显示pEf

比如想测试完生成报告,失败重跑两次,一共运行两次,通过分布式去测试,如果在cmd中写的话,命令会很长:

pytest -v --rerun=2 --count=2 --html=report.html --self-contained-html -n=auto

直接在配置中增加以下配置,执行的时候只需要输入 pytest 即可:

# 命令行参数
addopts = -v --reruns=1 --count=2 --html=reports.html --self-contained-html -n=auto

2.testpaths

需要执行的测试用例路径,可以指定某个包、某个模块或具体测试用例,那么执行命令 pytest 将不会执行根目录下所有,而是执行testpaths下所有用例

3.markers

测试用例中添加了@pytest.mark.webtest 装饰器,如果不添加markers选项的话,就会报warnings

4.norecursedirs

pytest 收集测试用例时,会递归遍历所有子目录,包括某些你明知道没必要遍历的目录,遇到这种情况,可以使用 norecursedirs 参数简化 pytest 的搜索工作【还是挺有用的!!!】

默认:norecursedirs = .* build dist CVS _darcs {arch} *.egg

多个路径空格隔开,如下:

[pytest]
norecursedirs = .* build dist CVS _darcs {arch} *.egg venv src resources log report util

5.xfail_strict

即xfail的strict参数,默认是False,如果为true,被装饰用例即使无报错,测试结果也是failed,不是xpassed

如果pytest.ini配置的xfail_strict和测试用例xfail装饰器自己的参数不一致,那么最终是以测试用例自己的为准,如测试用例指明strict=False,那么对于该用例配置文件的True就会失效

6.更改用例书写规则

pytest有默认的用例书写规则,当然也可以修改或者添加这个用例收集规则,建议在原有的规则上添加的,如下配置

[pytest]
python_files = test_* *_test test*
python_classes = Test* test*
python_functions = test_* test*

 

上一篇:pytest的参数化
下一篇:没有了
网友评论