前言
pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行
非test文件
pytest里面有些文件是非test文件
- pytest.ini:pytest的主配置文件,可以改变pytest的默认行为
- conftest.py:测试用例的一些fixture配置
- _init_.py:识别该文件夹为python的package包
查看pytest.ini的配置选项
cmd [ 终端执行 ]
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
pythonpath (paths): Add paths to sys.path
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
reruns (string): number of times to re-run failed tests. defaults to 0.
reruns_delay (string):
add time (seconds) delay between reruns.
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
pytest.ini应该放哪里?
就放在项目根目录下 ,不要乱放,不要乱起其他名字
接下来讲下常用的配置项 【marks、 addopts、 xfail_strict、 log_cli、 norecursedirs.....】
1. marks 【用例的别称】:
作用:测试用例中添加了 @pytest.mark.webtest 装饰器,如果不添加marks选项的话,就会报warnings
格式:list列表类型
写法:
[pytest]
markers =
weibo: this is weibo page
toutiao: toutiao
xinlang: xinlang
2. addopts 【 更改默认命令行选项 】:
作用:addopts参数可以更改默认命令行选项,这个当我们在cmd输入一堆指令去执行用例的时候,就可以用该参数代替了,省去重复性的敲命令工作
比如:想测试完生成报告,失败重跑两次,一共运行两次,通过分布式去测试,如果在cmd中写的话,命令会很长
pytest -v --rerun=2 --count=2 --html=report.html --self-contained-html -n=auto
-s: 输出调试信息,包括print打印的信息
-v: 显示更详细的信息
-q: 显示简略信息,与-v作用相反
-p no:warning:过滤警告
-p no:randomly:disable随机执行
-n=num: 启用多线程或分布式运行测试用例。需要安装pytest-xdist插件模块
-k=value: py文件中用例包含value的用例都会被执行
-m=标签: 执行被@pytest.mark.标签名标记的用例
-x: 只要有一个用例执行失败就停止当前线程的测试执行
–maxfail=num: 与-x功能一样,可以自定义用例失败次数
–rerun=num: 失败用例重跑,需要安装pytest-rerunfailures插件模块
-l: 展示运行过程中的全局变量和局部变量
–collect-only: 罗列出所有当前目录下所有的测试模块,测试类及测试函数
–ff: 如果上次测试用例出现失败的用例,当使用–ff后,失败的测试用例会首先执行,剩余的用例也会再次执行
–lf: 一个或多个用例失败后,定位到最后一个失败的用例重新运行,后续用例会停止运行
–html=report.html: 当前目录生成名为report.html的测试报告,需要安装pytest.html插件模块
有一个博主写了addopts 参数使用,我就懒得写了 :目前最火的测试框架,pytest封神级讲解_pytest addopts
每次都这样敲不太现实,addopts就可以完美解决这个问题
[pytest]
# mark
markers =
weibo: this is weibo page
toutiao: toutiao
xinlang: xinlang
xfail_strict = True
# 命令行参数
addopts = -v --reruns=1 --count=2 --html=reports.html --self-contained-html -n=auto
加了addopts之后,我们在cmd中只需要敲pytest就可以生效了!!
3. log_cli
作用:控制台实时输出日志
格式:log_cli=True 或False(默认),或者log_cli=1 或 0
log_cli=0的运行结果
log_cli=1的运行结果
结论
很明显,加了log_cli=1之后,可以清晰看到哪个package下的哪个module下的哪个测试用例是否passed还是failed;
所以平时测试代码是否有问题的情况下推荐加!!!但如果拿去批量跑测试用例的话不建议加,谁知道会不会影响运行性能呢?
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. 更改测试用例收集规则
pytest默认的测试用例收集规则
- 文件名以 test_*.py 文件和 *_test.py
- 以 test_ 开头的函数
- 以 Test 开头的类,不能包含 __init__ 方法
- 以 test_ 开头的类里面的方法
我们是可以修改或者添加这个用例收集规则的;当然啦,是建议在原有的规则上添加的,如下配置
[pytest]
python_files = test_* *_test test*
python_classes = Test* test*
python_functions = test_* test*