1、pytest.ini的放置位置:
一般放在项目工程的根目录(即当前项目的顶级文件夹下),名字不能更改
2、pytest.ini的作用:
指定pytest的运行方式(命令行窗口输入pytest后,会读取pytest.ini中的配置信息,按配置的方式去运行)
3、cmd下使用 pytest -h 命令查看pytest.ini的设置选项如下:
[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 render_collapsed (bool): Open the report with all rows collapsed. Useful for very large reports
4、常用配置项:
1)marks
测试用例中添加了 @pytest.mark.webtest 装饰器,如果不添加 marks 选项的话,就会报 warnings
格式:list列表类型
markers =
demo : marks tests as demo
smoke: marks tests as smoke
test : marks tests as test
2)addopts
addopts参数可以更改默认命令行选项,当我们需要在cmd输入一堆指令去执行用例的时候,就可以用该参数代替了,省去重复性的敲命令工作。多个参数用空格隔开
例:想测试完生成报告,失败重跑两次,一共运行两次,通过分布式去测试,测试显示xfail和skip,如果在cmd中写的话,命令会很长
pytest -v --rerun=2 --count=2 --html=report.html --self-contained-html -n=auto
使用addopts增加默认配置,再次运行时,输入 pytest 会默认执行下面命令:
addopts = -rsxX -v --reruns=1 --count=2 --html=reports.html --self-contained-html -n=auto
3)log_cli
控制台实时输出日志
格式:log_cli=True 或False(默认),或者log_cli=1 或 0
pytest 收集测试用例时,会递归遍历所有子目录,包括某些你明知道没必要遍历的目录,可以使用 norecursedirs 参数简化 pytest 的搜索工作。多个路径用空格隔开
默认设置: norecursedirs = .* build dist CVS _darcs {arch} *.egg
norecursedirs = .* build dist CVS _darcs {arch} *.egg venv src resources log report util
6)更改用例的收集规则:
pytest默认的测试用例收集规则:
- 文件名以 test_*.py 文件和 *_test.py
- 以 test_ 开头的函数
- 以 Test 开头的类,不能包含 __init__ 方法
- 以 test_ 开头的类里面的方法
修改如下配置项,来改变pytest收集用例默认的规则(建议在原基础上修改):
python_files = test_* *_test test* python_classes = Test* test* python_functions = test_* test*