> 文档中心 > pytest系列教程——10、配置文件pytest.ini

pytest系列教程——10、配置文件pytest.ini

文章目录

      • addopts
      • testpaths
      • norecursedirs
      • marks
      • log_cli

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

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

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

C:\Users\xxx> pytest  -husage: pytest [options] [file_or_dir] [file_or_dir] [...]positional arguments:file_or_dirgeneral:-k EXPRESSION  only run tests which match the given substring expression. An expression is a python evaluatableexpression where all names are substring-matched against test names and their parent classes.Example: -k 'test_method or test_other' matches all test functions and classes whose namecontains 'test_method' or 'test_other', while -k 'not test_method' matches those that don'tcontain 'test_method' in their names. -k 'not test_method and not test_other' will eliminate thematches. Additionally keywords are matched to classes and functions containing extra names intheir 'extra_keyword_matches' set, as well as functions which have names assigned directly tothem. The matching is case-insensitive.-m MARKEXPR    only run tests matching given mark expression.For example: -m 'mark1 and not mark2'.--markers      show markers (builtin, plugin and per-project ones).-x, --exitfirstexit instantly on first error or failed test.--fixtures, --funcargsshow available fixtures, sorted by plugin appearance (fixtures with leading '_' are only shownwith '-v')--fixtures-per-test   show fixtures per test--pdb   start the interactive Python debugger on errors or KeyboardInterrupt.--pdbcls=modulename:classnamestart a custom interactive Python debugger on errors. For example:--pdbcls=IPython.terminal.debugger:TerminalPdb--trace Immediately break when running each test.--capture=method      per-test capturing method: one of fd|sys|no|tee-sys.-s      shortcut for --capture=no.--runxfail     report the results of xfail tests as if they were not marked--lf, --last-failed   rerun only the tests that failed at the last run (or all if none failed)--ff, --failed-first  run all tests, but run the last failures first.This may re-order tests and thus lead to repeated fixture setup/teardown.--nf, --new-first     run tests from new files first, then the rest of the tests sorted by file mtime--cache-show=[CACHESHOW]show cache contents, don't perform collection or tests. Optional argument: glob (default: '*').--cache-clear  remove all cache contents at start of test run.--lfnf={all,none}, --last-failed-no-failures={all,none}which tests to run with no previously (known) failures.--sw, --stepwise      exit on test failure and continue from last failing test next time--sw-skip, --stepwise-skip... ...... ...

addopts

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

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

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

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

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

testpaths

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

[pytest]testpaths =  path1 path2

norecursedirs

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

[pytest]norecursedirs = logs .pytest_cache __pycache__  setting

marks

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

[pytest]markers = IOS: run in ios Android:run in AndroidsmokeTest:smokeTest case

log_cli

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

[pytest]log_cli=True