在第一节的时候,讲过,关于pytest执行的参数命令,比如执行用例的时候加上-s参数,就能显示打印内容。-x 遇到错误时停止测试 ,–maxfail=1出现1个失败就终止测试,等等运行条件

  1. if __name__ == '__main__':
  2. pytest.main(["-s -x", "./","--maxfail=1"])

如此多的运行参数,无论是从阅读还是配置都增加了难度。为了让项目运行方式便于管理,pytest专门提供了pytest.ini文件,可以轻松解决上述问题。
pytest.ini 是一个固定文件,也就是说名称不能更改,而且必须存放在项目根目录下。
cmd下使用 pytest -h 命令查看pytest.ini的设置选项

  1. C:\Users\xxx> pytest -h
  2. usage: pytest [options] [file_or_dir] [file_or_dir] [...]
  3. positional arguments:
  4. file_or_dir
  5. general:
  6. -k EXPRESSION only run tests which match the given substring expression. An expression is a python evaluatable
  7. expression where all names are substring-matched against test names and their parent classes.
  8. Example: -k 'test_method or test_other' matches all test functions and classes whose name
  9. contains 'test_method' or 'test_other', while -k 'not test_method' matches those that don't
  10. contain 'test_method' in their names. -k 'not test_method and not test_other' will eliminate the
  11. matches. Additionally keywords are matched to classes and functions containing extra names in
  12. their 'extra_keyword_matches' set, as well as functions which have names assigned directly to
  13. them. The matching is case-insensitive.
  14. -m MARKEXPR only run tests matching given mark expression.
  15. For example: -m 'mark1 and not mark2'.
  16. --markers show markers (builtin, plugin and per-project ones).
  17. -x, --exitfirst exit instantly on first error or failed test.
  18. --fixtures, --funcargs
  19. show available fixtures, sorted by plugin appearance (fixtures with leading '_' are only shown
  20. with '-v')
  21. --fixtures-per-test show fixtures per test
  22. --pdb start the interactive Python debugger on errors or KeyboardInterrupt.
  23. --pdbcls=modulename:classname
  24. start a custom interactive Python debugger on errors. For example:
  25. --pdbcls=IPython.terminal.debugger:TerminalPdb
  26. --trace Immediately break when running each test.
  27. --capture=method per-test capturing method: one of fd|sys|no|tee-sys.
  28. -s shortcut for --capture=no.
  29. --runxfail report the results of xfail tests as if they were not marked
  30. --lf, --last-failed rerun only the tests that failed at the last run (or all if none failed)
  31. --ff, --failed-first run all tests, but run the last failures first.
  32. This may re-order tests and thus lead to repeated fixture setup/teardown.
  33. --nf, --new-first run tests from new files first, then the rest of the tests sorted by file mtime
  34. --cache-show=[CACHESHOW]
  35. show cache contents, don't perform collection or tests. Optional argument: glob (default: '*').
  36. --cache-clear remove all cache contents at start of test run.
  37. --lfnf={all,none}, --last-failed-no-failures={all,none}
  38. which tests to run with no previously (known) failures.
  39. --sw, --stepwise exit on test failure and continue from last failing test next time
  40. --sw-skip, --stepwise-skip
  41. ignore the first failing test but stop on the next failing test
  42. --allure-severities=SEVERITIES_SET
  43. Comma-separated list of severity names.
  44. Tests only with these severities will be run.
  45. Possible values are: blocker, critical, normal, minor, trivial.
  46. --allure-epics=EPICS_SET
  47. Comma-separated list of epic names.
  48. Run tests that have at least one of the specified feature labels.
  49. --allure-features=FEATURES_SET
  50. Comma-separated list of feature names.
  51. Run tests that have at least one of the specified feature labels.
  52. --allure-stories=STORIES_SET
  53. Comma-separated list of story names.
  54. Run tests that have at least one of the specified story labels.
  55. --allure-ids=IDS_SET Comma-separated list of IDs.
  56. Run tests that have at least one of the specified id labels.
  57. --allure-link-pattern=LINK_TYPE:LINK_PATTERN
  58. Url pattern for link type. Allows short links in test,
  59. like 'issue-1'. Text will be formatted to full url with python
  60. str.format().
  61. reporting:
  62. --durations=N show N slowest setup/test durations (N=0 for all).
  63. --durations-min=N Minimal duration in seconds for inclusion in slowest list. Default 0.005
  64. -v, --verbose increase verbosity.
  65. --no-header disable header
  66. --no-summary disable summary
  67. -q, --quiet decrease verbosity.
  68. --verbosity=VERBOSE set verbosity. Default is 0.
  69. -r chars show extra test summary info as specified by chars: (f)ailed, (E)rror, (s)kipped, (x)failed,
  70. (X)passed, (p)assed, (P)assed with output, (a)ll except passed (p/P), or (A)ll. (w)arnings are
  71. enabled by default (see --disable-warnings), 'N' can be used to reset the list. (default: 'fE').
  72. --disable-warnings, --disable-pytest-warnings
  73. disable warnings summary
  74. -l, --showlocals show locals in tracebacks (disabled by default).
  75. --tb=style traceback print mode (auto/long/short/line/native/no).
  76. --show-capture={no,stdout,stderr,log,all}
  77. Controls how captured stdout/stderr/log is shown on failed tests. Default is 'all'.
  78. --full-trace don't cut any tracebacks (default is to cut).
  79. --color=color color terminal output (yes/no/auto).
  80. --code-highlight={yes,no}
  81. Whether code should be highlighted (only if --color is also enabled)
  82. --pastebin=mode send failed|all info to bpaste.net pastebin service.
  83. --junit-xml=path create junit-xml style report file at given path.
  84. --junit-prefix=str prepend prefix to classnames in junit-xml output
  85. --html=path create html report file at given path.
  86. --self-contained-html
  87. create a self-contained html file containing all necessary styles, scripts, and images - this
  88. means that the report may not render or function where CSP restrictions are in place (see
  89. https://developer.mozilla.org/docs/Web/Security/CSP)
  90. --css=path append given css file content to report style file.
  91. pytest-warnings:
  92. -W PYTHONWARNINGS, --pythonwarnings=PYTHONWARNINGS
  93. set which warnings to report, see -W option of python itself.
  94. --maxfail=num exit after first num failures or errors.
  95. --strict-config any warnings encountered while parsing the `pytest` section of the configuration file raise
  96. errors.
  97. --strict-markers markers not registered in the `markers` section of the configuration file raise errors.
  98. --strict (deprecated) alias to --strict-markers.
  99. -c file load configuration from `file` instead of trying to locate one of the implicit configuration
  100. files.
  101. --continue-on-collection-errors
  102. Force test execution even if collection errors occur.
  103. --rootdir=ROOTDIR Define root directory for tests. Can be relative path: 'root_dir', './root_dir',
  104. 'root_dir/another_dir/'; absolute path: '/home/user/root_dir'; path with variables:
  105. '$HOME/root_dir'.
  106. collection:
  107. --collect-only, --co only collect tests, don't execute them.
  108. --pyargs try to interpret all arguments as python packages.
  109. --ignore=path ignore path during collection (multi-allowed).
  110. --ignore-glob=path ignore path pattern during collection (multi-allowed).
  111. --deselect=nodeid_prefix
  112. deselect item (via node id prefix) during collection (multi-allowed).
  113. --confcutdir=dir only load conftest.py's relative to specified dir.
  114. --noconftest Don't load any conftest.py files.
  115. --keep-duplicates Keep duplicate tests.
  116. --collect-in-virtualenv
  117. Don't ignore tests in a local virtualenv directory
  118. --import-mode={prepend,append,importlib}
  119. prepend/append to sys.path when importing test modules and conftest files, default is to
  120. prepend.
  121. --doctest-modules run doctests in all .py modules
  122. --doctest-report={none,cdiff,ndiff,udiff,only_first_failure}
  123. choose another output format for diffs on doctest failure
  124. --doctest-glob=pat doctests file matching pattern, default: test*.txt
  125. --doctest-ignore-import-errors
  126. ignore doctest ImportErrors
  127. --doctest-continue-on-failure
  128. for a given doctest, continue to run after the first failure
  129. test session debugging and configuration:
  130. --basetemp=dir base temporary directory for this test run.(warning: this directory is removed if it exists)
  131. -V, --version display pytest version and information about plugins.When given twice, also display information
  132. about plugins.
  133. -h, --help show help message and configuration info
  134. -p name early-load given plugin module name or entry point (multi-allowed).
  135. To avoid loading of plugins, use the `no:` prefix, e.g. `no:doctest`.
  136. --trace-config trace considerations of conftest.py files.
  137. --debug store internal tracing debug information in 'pytestdebug.log'.
  138. -o OVERRIDE_INI, --override-ini=OVERRIDE_INI
  139. override ini option with "option=value" style, e.g. `-o xfail_strict=True -o cache_dir=cache`.
  140. --assert=MODE Control assertion debugging tools.
  141. 'plain' performs no assertion debugging.
  142. 'rewrite' (the default) rewrites assert statements in test modules on import to provide assert
  143. expression information.
  144. --setup-only only setup fixtures, do not execute tests.
  145. --setup-show show setup of fixtures while executing tests.
  146. --setup-plan show what fixtures and tests would be executed but don't execute anything.
  147. logging:
  148. --log-level=LEVEL level of messages to catch/display.
  149. Not set by default, so it depends on the root/parent log handler's effective level, where it is
  150. "WARNING" by default.
  151. --log-format=LOG_FORMAT
  152. log format as used by the logging module.
  153. --log-date-format=LOG_DATE_FORMAT
  154. log date format as used by the logging module.
  155. --log-cli-level=LOG_CLI_LEVEL
  156. cli logging level.
  157. --log-cli-format=LOG_CLI_FORMAT
  158. log format as used by the logging module.
  159. --log-cli-date-format=LOG_CLI_DATE_FORMAT
  160. log date format as used by the logging module.
  161. --log-file=LOG_FILE path to a file when logging will be written to.
  162. --log-file-level=LOG_FILE_LEVEL
  163. log file logging level.
  164. --log-file-format=LOG_FILE_FORMAT
  165. log format as used by the logging module.
  166. --log-file-date-format=LOG_FILE_DATE_FORMAT
  167. log date format as used by the logging module.
  168. --log-auto-indent=LOG_AUTO_INDENT
  169. Auto-indent multiline messages passed to the logging module. Accepts true|on, false|off or an
  170. integer.
  171. reporting:
  172. --alluredir=DIR Generate Allure report in the specified directory (may not exist)
  173. --clean-alluredir Clean alluredir folder if it exists
  174. --allure-no-capture Do not attach pytest captured logging/stdout/stderr to report
  175. forked subprocess test execution:
  176. --forked box each test run in a separate process (unix)
  177. re-run failing tests to eliminate flaky failures:
  178. --only-rerun=ONLY_RERUN
  179. If passed, only rerun errors matching the regex provided. Pass this flag multiple times to
  180. accumulate a list of regexes to match
  181. --reruns=RERUNS number of times to re-run failed tests. defaults to 0.
  182. --reruns-delay=RERUNS_DELAY
  183. add time (seconds) delay between reruns.
  184. distributed and subprocess testing:
  185. -n numprocesses, --numprocesses=numprocesses
  186. Shortcut for '--dist=load --tx=NUM*popen'. With 'auto', attempt to detect physical CPU count.
  187. With 'logical', detect logical CPU count. If physical CPU count cannot be found, falls back to
  188. logical count. This will be 0 when used with --pdb.
  189. --maxprocesses=maxprocesses
  190. limit the maximum number of workers to process the tests when using --numprocesses=auto
  191. --max-worker-restart=MAXWORKERRESTART
  192. maximum number of workers that can be restarted when crashed (set to zero to disable this
  193. feature)
  194. --dist=distmode set mode for distributing tests to exec environments.
  195. each: send each test to all available environments.
  196. load: load balance by sending any pending test to any available environment.
  197. loadscope: load balance by sending pending groups of tests in the same scope to any available
  198. environment.
  199. loadfile: load balance by sending test grouped by file to any available environment.
  200. (default) no: run tests inprocess, don't distribute.
  201. --tx=xspec add a test execution environment. some examples: --tx popen//python=python2.5 --tx
  202. socket=192.168.1.102:8888 --tx ssh=user@codespeak.net//chdir=testcache
  203. -d load-balance tests. shortcut for '--dist=load'
  204. --rsyncdir=DIR add directory for rsyncing to remote tx nodes.
  205. --rsyncignore=GLOB add expression for ignores when rsyncing to remote tx nodes.
  206. --boxed backward compatibility alias for pytest-forked --forked
  207. --testrunuid=TESTRUNUID
  208. provide an identifier shared amongst all workers as the value of the 'testrun_uid' fixture,
  209. ,if not provided, 'testrun_uid' is filled with a new unique string on every test run.
  210. -f, --looponfail run tests in subprocess, wait for modified files and re-run failing test set until all pass.

addopts

addopts就可以专门解决开篇讲的问题。addopts 参数可以更改默认命令行选项,这个当我们在 cmd 输入一堆指令去执行用例的时候,就可以用该参数代替了,省去重复性的敲命令工作

  1. [pytest]
  2. addopts = -s -x --maxfail=1

文章开头的运行代码则可以调整为:

  1. if __name__ == '__main__':
  2. pytest.main(["./"])

addopts只是他的一种用法,还有其他一些经典用法。

testpaths

指定运行的测试用例目录。
如果需要执行多个目录下的,则需要在各个目录中间加入空格。例如运行path1 和 path2 路径下的所有用例:

  1. [pytest]
  2. testpaths = path1 path2

norecursedirs

指定不运行的测试用例目录,和testpaths的效果正好相反,如果是多个目录则用空格隔开。

  1. [pytest]
  2. norecursedirs = logs .pytest_cache __pycache__ setting

marks

在之前讲mark 标记测试用例一节的时候,就提到,为了解决使用@pytest.mark.xxx会出现warning的情况,需要在pytest.ini文件对定义的mark进行解释

  1. [pytest]
  2. markers =
  3. IOS: run in ios
  4. Android: run in Android
  5. smokeTest: smokeTest case

log_cli

控制日志文件的输出级别,log_cli=True 输出较为详细日志,包括运行路径。log_cli=False, 输出粗略日志。建议使用log_cli=True

  1. [pytest]
  2. log_cli=True