前言:
pytest.ini文件是pytest的主配置文件,可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行。
pytest.ini文件的位置一般放在项目的根目录下,不能随便放,也不能更改名字。
查看pytest.ini文件的配置选项
cmd下执行pytest -h 或者 pytest --help 可以查看配置选项,找到如下内容:
[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.
filterwarnings (linelist):
Each line specifies a pattern for warnings.filterwarnings. Processed after -W/--pythonwarnings.
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.
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.
addopts (args): extra command line options
minversion (string): minimally required pytest version
required_plugins (args):
plugins that must be present for pytest to run
render_collapsed (bool):
Open the report with all rows collapsed. Useful for very large reports
max_asset_filename_length (string):
set the maximum filename length for assets attached to the html report.
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
常用的配置选项
markers
作用:测试用例中使用@pytest.mark.slow装饰器,如果不添加markers选项就会报warning
格式:list列表类型
写法:
# file_name: pytest.ini
[pytest]
markers =
slow: run slow mark case
fast: run fast mark case
xfail_strict
作用:设置xfail_strict=true可以让那些标记为@pytest.mark.xfail 但实际通过显示为XPASS 的测试用例被报告为失败
格式:True、False(默认)、1、0
写法:
# file_name: pytest.ini
[pytest]
markers =
slow: run slow mark case
fast: run fast mark case
xfail_strict = true
举例说明:
# file_name: test_xfail.py
import pytest
class Test_C:
@pytest.mark.xfail
def test_c(self):
print('\n------------------> test_c has ran')
a = 'hello'
b = 'hello world'
assert a != b
if __name__ == '__main__':
pytest.main(['-s', 'test_xfail.py'])
没有设置xfail_strict = True 时,测试结果显示XPASS
设置xfail_strict = True 时,测试结果显示failed
addopts
作用:addopts参数可以更改默认的命令行选项,这个参数在我们需要在命令行中输入大一堆指令来执行测试用例时会用到,这个时候就可以在配置文件中配置这个参数来代替,省掉很多重复的工作
例如:我想在测试结束之后,生成测试报告,失败的测试用例重跑两次,如果通过命令行输入指令来执行的话,指令会很长:
pytest -v --reruns=2 --html=report.html --self-contained-html
如果每次执行都要输入上面的指令会很繁琐,这个时候我们通过配置addopts参数来解决这个问题:
# file_name: pytest.ini
[pytest]
markers =
slow: run slow mark case
fast: run fast mark case
xfail_strict = True
addopts = -v --reruns=2 --html=report.html --self-contained-html # 多个命令行参数用空格分隔开,可以添加多个命令行参数
这样加上addopts后,我们再次进入cmd命令行执行时,只要输入pytest就可以默认带上这些参数了。
log_cli
作用:控制台实时输出日志
格式:log_cli=True 或False(默认),或者log_cli=1 或 0
设置log_cli=True时,运行结果为:
设置log_cli=False时,运行结果为:
结论:当我们设置log_cli=True时,可以非常清晰的看出具体的是哪个package下的哪个module下的哪个测试用例是passed还是failed;
所以我们平时在调试代码是否有问题时推荐加上log_cli=True,当测试用例调试通过之后批量执行时就可以去掉了。
norecursedirs
作用:pytest在收集测试用例的时候,会递归遍历当前目录下的所有子目录,当我们需要某些目录下的用例不要执行时,就可以通过设置norecursedirs参数来实现这个功能。
# file_name: pytest.ini
[pytest]
markers =
slow: run slow mark case
fast: run fast mark case
xfail_strict = True
addopts = -v --reruns=2 --html=report.html --self-contained-html
log_cli = False
norecursedirs = venv report util log # 多个目录需要空格分开,可以配置多个
上面的配置表示venv report util log这4个目录下的用例需要过滤掉不执行。
更改测试用例的收集规则
pytest默认的测试用例收集规则为:
- 文件名匹配test_*.py或*_test.py
- 以test_开头的函数
- 以Test_开头的类,不能包含_init_方法
- 类中以test_开头的方法
上面的默认规则我们是可以通过配置文件的设置来修改的
# file_name: pytest.ini
[pytest]
testpaths = xdist_study
python_files = test*.py
python_classes = Test*
python_functions = test_*
testpaths:配置在哪个目录下搜索测试用例,可自定义,可以配置多个,多个用空格隔开
python_files:用来配置搜索的测试用例的文件名称,可自定义,可以配置多个,多个用空格隔开
python_classes:配置搜索的测试用例的类名,可自定义,可以配置多个,多个用空格隔开
python_functions:配置搜索的测试用例的方法名,可自定义,可以配置多个,多个用空格隔开
上面的配置表示:在xdist_study目录下,搜索以test开头,以.py结尾的文件,以Test开头的类,以test_开头的方法。
总结:
感谢每一个认真阅读我文章的人!!!
我个人整理了我这几年软件测试生涯整理的一些技术资料,包含:电子书,简历模块,各种工作模板,面试宝典,自学项目等。欢迎大家评论区留言333免费领取,千万不要错过哦。