前言

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

非test文件

pytest里面有些文件是非test文件

  • pytest.ini:pytest的主配置文件,可以改变pytest的默认行为
  • conftest.py:测试用例的一些fixture配置
  • init.py:识别该文件夹为python的package包

查看pytest.ini的配置选项

cmd执行

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

pytest.ini应该放哪里?

就放在项目根目录下 ,不要乱放,不要乱起其他名字

接下来讲下常用的配置项

marks

作用:测试用例中添加了 @pytest.mark.webtest 装饰器,如果不添加marks选项的话,就会报warnings
格式:list列表类型
写法:

  1. [pytest]
  2. markers =
  3. weibo: this is weibo page
  4. toutiao: toutiao
  5. xinlang: xinlang

xfail_strict

作用:设置xfail_strict = True可以让那些标记为@pytest.mark.xfail但实际通过显示XPASS的测试用例被报告为失败
格式:True 、False(默认),1、0
写法:

  1. [pytest]
  2. # mark标记说明
  3. markers =
  4. weibo: this is weibo page
  5. toutiao: toutiao
  6. xinlang: xinlang
  7. xfail_strict = True


具体代码栗子
未设置 xfail_strict = True 时,测试结果显示XPASS

  1. @pytest.mark.xfail()
  2. def test_case1():
  3. a = "a"
  4. b = "b"
  5. assert a != b
  1. collecting ... collected 1 item
  2. 02断言异常.py::test_case1 XPASS [100%]
  3. ============================= 1 xpassed in 0.02s ==============================


已设置 xfail_strict = True 时,测试结果显示failed

  1. collecting ... collected 1 item
  2. 02断言异常.py::test_case1 FAILED [100%]
  3. 02断言异常.py:54 (test_case1)
  4. [XPASS(strict)]
  5. ================================== FAILURES ===================================
  6. _________________________________ test_case1 __________________________________
  7. [XPASS(strict)]
  8. =========================== short test summary info ===========================
  9. FAILED 02断言异常.py::test_case1
  10. ============================== 1 failed in 0.02s ==============================

addopts

作用:addopts参数可以更改默认命令行选项,这个当我们在cmd输入一堆指令去执行用例的时候,就可以用该参数代替了,省去重复性的敲命令工作
比如:想测试完生成报告,失败重跑两次,一共运行两次,通过分布式去测试,如果在cmd中写的话,命令会很长

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


每次都这样敲不太现实,addopts就可以完美解决这个问题

  1. [pytest]
  2. # mark
  3. markers =
  4. weibo: this is weibo page
  5. toutiao: toutiao
  6. xinlang: xinlang
  7. xfail_strict = True
  8. # 命令行参数
  9. addopts = -v --reruns=1 --count=2 --html=reports.html --self-contained-html -n=auto

加了addopts之后,我们在cmd中只需要敲pytest就可以生效了!!

log_cli

作用:控制台实时输出日志
格式:log_cli=True 或False(默认),或者log_cli=1 或 0

log_cli=0的运行结果

Pytest系列(14)- 配置文件pytest.ini的详细使用 - 图1

log_cli=1的运行结果

Pytest系列(14)- 配置文件pytest.ini的详细使用 - 图2

结论

很明显,加了log_cli=1之后,可以清晰看到哪个package下的哪个module下的哪个测试用例是否passed还是failed;
所以平时测试代码是否有问题的情况下推荐加!!!但如果拿去批量跑测试用例的话不建议加,谁知道会不会影响运行性能呢?

norecursedirs

作用:pytest 收集测试用例时,会递归遍历所有子目录,包括某些你明知道没必要遍历的目录,遇到这种情况,可以使用 norecursedirs 参数简化 pytest 的搜索工作【还是挺有用的!!!】
默认设置: norecursedirs = . build dist CVS _darcs {arch} .egg
正确写法:多个路径用空格隔开

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

更改测试用例收集规则

pytest默认的测试用例收集规则

  • 文件名以 test_.py 文件和 _test.py
  • 以 test_ 开头的函数
  • 以 Test 开头的类,不能包含 init 方法
  • 以 test_ 开头的类里面的方法


    我们是可以修改或者添加这个用例收集规则的;当然啦,是建议在原有的规则上添加的,如下配置

    1. [pytest]
    2. python_files = test_* *_test test*
    3. python_classes = Test* test*
    4. python_functions = test_* test*

转载:https://www.cnblogs.com/poloyy/p/12702294.html