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

顺便一提,欢迎加入 CSDN软件测试社区 。如果有任何问题,你可以在这里留言

在第一节的时候,讲过,关于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  -h
usage: pytest [options] [file_or_dir] [file_or_dir] [...]

positional arguments:
file_or_dir

general:
-k EXPRESSION         only run tests which match the given substring expression. An expression is a python evaluatable
expression 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 name
contains 'test_method' or 'test_other', while -k 'not test_method' matches those that don't
contain 'test_method' in their names. -k 'not test_method and not test_other' will eliminate the
matches. Additionally keywords are matched to classes and functions containing extra names in
their 'extra_keyword_matches' set, as well as functions which have names assigned directly to
them. 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, --exitfirst       exit instantly on first error or failed test.
--fixtures, --funcargs
show available fixtures, sorted by plugin appearance (fixtures with leading '_' are only shown
with '-v')
--fixtures-per-test   show fixtures per test
--pdb                 start the interactive Python debugger on errors or KeyboardInterrupt.
--pdbcls=modulename:classname
start 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 Android
	smokeTest:	smokeTest case

log_cli

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

[pytest]

log_cli=True

顺便一提,欢迎加入 CSDN软件测试社区 。如果有任何问题,你可以在这里留言

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

软件测试技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值