18-pytest-配置文件pytest.ini使用

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: 18-pytest-配置文件pytest.ini使用

前言

  • pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行

查看选项

  • pytest  --help
1. [pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg file found:
2. 
3.   markers (linelist):   markers for test functions
4.   empty_parameter_set_mark (string):
5.                         default marker for empty parametersets
6.   norecursedirs (args): directory patterns to avoid for recursion
7.   testpaths (args):     directories to search for tests when no files or directories are given in the command line.
8.   filterwarnings (linelist):
9.                         Each line specifies a pattern for warnings.filterwarnings. Processed after -W/--pythonwarnings.
10.   usefixtures (args):   list of default fixtures to be used with this project
11.   python_files (args):  glob-style file patterns for Python test module discovery
12.   python_classes (args):
13.                         prefixes or glob names for Python test class discovery
14.   python_functions (args):
15.                         prefixes or glob names for Python test function and method discovery
16.   disable_test_id_escaping_and_forfeit_all_rights_to_community_support (bool):
17.                         disable string escape non-ascii characters, might cause unwanted side effects(use at your own
18.                         risk)
19.   console_output_style (string):
20.                         console output: "classic", or with additional progress information ("progress" (percentage) |
21. "count").
22.   xfail_strict (bool):  default for the strict parameter of xfail markers when not given explicitly (default: False)
23.   enable_assertion_pass_hook (bool):
24.                         Enables the pytest_assertion_pass hook.Make sure to delete any previously generated pyc cache
25.                         files.
26.   junit_suite_name (string):
27.                         Test suite name for JUnit report
28.   junit_logging (string):
29.                         Write captured log messages to JUnit report: one of no|log|system-out|system-err|out-err|all
30.   junit_log_passing_tests (bool):
31.                         Capture log information for passing tests to JUnit report:
32.   junit_duration_report (string):
33.                         Duration time to report: one of total|call
34.   junit_family (string):
35.                         Emit XML for schema: one of legacy|xunit1|xunit2
36.   doctest_optionflags (args):
37.                         option flags for doctests
38.   doctest_encoding (string):
39.                         encoding used for doctest files
40.   cache_dir (string):   cache directory path.
41.   log_level (string):   default value for --log-level
42.   log_format (string):  default value for --log-format
43.   log_date_format (string):
44.                         default value for --log-date-format
45.   log_cli (bool):       enable log display during test run (also known as "live logging").
46.   log_cli_level (string):
47.                         default value for --log-cli-level
48.   log_cli_format (string):
49.                         default value for --log-cli-format
50.   log_cli_date_format (string):
51.                         default value for --log-cli-date-format
52.   log_file (string):    default value for --log-file
53.   log_file_level (string):
54.                         default value for --log-file-level
55.   log_file_format (string):
56.                         default value for --log-file-format
57.   log_file_date_format (string):
58.                         default value for --log-file-date-format
59.   log_auto_indent (string):
60.                         default value for --log-auto-indent
61.   faulthandler_timeout (string):
62.                         Dump the traceback of all threads if a test takes more than TIMEOUT seconds to finish.
63.   addopts (args):       extra command line options
64.   minversion (string):  minimally required pytest version
65.   required_plugins (args):
66.                         plugins that must be present for pytest to run
67.   render_collapsed (bool):
68.                         Open the report with all rows collapsed. Useful for very large reports
69.   max_asset_filename_length (string):
70. set the maximum filename length for assets attached to the html report.
71.   rsyncdirs (pathlist): list of (relative) paths to be rsynced for remote distributed testing.
72.   rsyncignore (pathlist):
73. list of (relative) glob-style paths to be ignored for rsyncing.
74.   looponfailroots (pathlist):
75.                         directories to check for changes

使用方式

  • 固定命名:pytest.ini (不要改名字)
  • 存放位置:放在项目根目录下

常用配置项

  • markers:注册自定义标签,主要解决自定义标记的warnings提示信息。
  • xfail_strict:让那些标记为@pytest.mark.xfail但实际通过显示XPASS的测试用例被报告为失败
  • addops:命令行参数
  • log_cli:控制台输出日志
  • norecursedirs:收集测试用例时,跳过的目录
1. # pytest.ini 
2. 
3. [pytest]
4. 
5. # 命令行参数 -v 详细打印 -s 打印调试信息   失败重跑两次   一共运行两次
6. addopts = -v -s --reruns=1 --count=2
7. 
8. # 注册mark
9. markers=
10.     web: web 测试用例
11.     app: app 测试用例
12. 
13. # 指定测试case路径
14. testpaths = testcase/
15. 
16. # 忽略的搜索路径
17. norecursedirs = evn reports/
18. 
19. # 搜索用例规则,测试类、测试文件、测试方法
20. python_files =     test_*  *_test  test*
21. python_classes =   Test*   test*
22. python_functions = test_*  test*
23. 
24. # 控制台输出日志
25. log_cli=1
26. 
27. # 设置控制台日志信息
28. log_cli_level=info
29. log_cli_date_format=%Y-%m-%d-%H-%M-%S
30. log_cli_format=%(asctime)s-%(filename)s-%(module)s-%(funcName)s-%(lineno)d-%(levelname)s-%(message)s
31. 
32. # 设置日志文件信息
33. log_file=test.log
34. log_file_level=INFO
35. log_file_date_format=%Y-%m-%d %H:%M:%S
36. log_file_format=%(asctime)s %(filename)s %(module)s %(funcName)s %(lineno)d %(levelname)s: %(message)s

注意事项

  • pytest.ini 文件里面不能使用任何中文符号,包括汉字、空格、引号、冒号等;


相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
6月前
|
测试技术 C++ Windows
Pytest配置文件pytest.ini
`pytest.ini`是pytest配置文件,用于修改默认行为,不支持中文符号。配置内容包括:定制测试文件、类和方法的命名规则,添加默认参数(如`addopts = -v -s --alluredir=./results`),指定或忽略执行目录,以及设置日志格式和级别。通过配置,可以更方便地管理测试运行和日志收集。
45 1
|
6月前
|
Linux Python Windows
python安装pytest
【4月更文挑战第22天】
93 5
|
测试技术 Python
Pytest3种配置文件方式
Pytest3种配置文件方式
56 0
|
6月前
|
测试技术 Python
pytest中的fixture和conftest.py
pytest中的fixture和conftest.py
|
测试技术
配置文件pytest.ini的详细使用
配置文件pytest.ini的详细使用
116 0
|
测试技术 Python
|
测试技术 Python
|
测试技术
pytest--常用插件
pytest--常用插件
|
JSON 测试技术 数据格式
19-pytest-allure-pytest环境搭建
19-pytest-allure-pytest环境搭建