配置文件pytest.ini
前言
pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行。
ini配置文件
pytest里面有些文件是非test文件
- pytest.ini pytest的主配置文件,可以改变pytest的默认行为
- conftest.py 测试用例的一些fixture配置
- __init__.py 识别该文件夹为python的package包
- tox.ini 与pytest.ini类似,用tox工具时候才有用
- setup.cfg 也是ini格式文件,影响setup.py的行为
ini文件基本格式
# 保存为pytest.ini文件 [pytest] addopts = -rsxX
xfail_strict = true
使用pytest --help指令可以查看pytest.ini的设置选项
[pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg file found: markers (linelist) markers for test functions
empty_parameter_set_mark (string) default marker for empty parametersets
norecursedirs (args) directory patterns to avoid for recursion
testpaths (args) directories to search for tests when no files or dire console_output_style (string) console output: classic or with additional progr usefixtures (args) list of default fixtures to be used with this project python_files (args) glob-style file patterns for Python test module disco python_classes (args) prefixes or glob names for Python test class discover python_functions (args) prefixes or glob names for Python test function and m xfail_strict (bool) default for the strict parameter of
addopts (args) extra command line options
minversion (string) minimally required pytest version
--rsxX 表示pytest报告所有测试用例被跳过、预计失败、预计失败但实际被通过的原因
mark标记
如下案例,使用了2个标签:webtest和hello,使用mark标记功能对于以后分类测试非常有用处
# content of test_mark.py
import pytest @pytest.mark.webtest
def test_send_http():
print("mark web test") def test_something_quick():
pass def test_another():
pass @pytest.mark.hello
class TestClass:
def test_01(self):
print("hello :") def test_02(self):
print("hello world!") if __name__ == "__main__":
pytest.main(["-v", "test_mark.py", "-m=hello"])
运行结果
============================= test session starts =============================
platform win32 -- Python 3.6., pytest-3.6., py-1.5., pluggy-0.6. -- D:\soft\python3.\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.0', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '3.6.3', 'py': '1.5.4', 'pluggy': '0.6.0'}, 'Plugins': {'metadata': '1.7.0', 'html': '1.19.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'D:\\soft\\jdk18\\jdk18v'}
rootdir: D:\YOYO, inifile:
plugins: metadata-1.7., html-1.19., allure-adaptor-1.7.
collecting ... collected items / deselected test_mark.py::TestClass::test_01 PASSED [ %]
test_mark.py::TestClass::test_02 PASSED [%] =================== passed, deselected in 0.11 seconds ====================
有时候标签多了,不容易记住,为了方便后续执行指令的时候能准确使用mark的标签,可以写入到pytest.ini文件
# pytest.ini
[pytest] markers =
webtest: Run the webtest case
hello: Run the hello case
标记好之后,可以使用pytest --markers查看到
$ pytest --markers
D:\YOYO>pytest --markers
@pytest.mark.webtest: Run the webtest case @pytest.mark.hello: Run the hello case @pytest.mark.skip(reason=None): skip the given test function with an optional re
ason. Example: skip(reason="no way of currently testing this") skips the test. @pytest.mark.skipif(condition): skip the given test function if eval(condition)
results in a True value. Evaluation happens within the module global context. E
xample: skipif('sys.platform == "win32"') skips the test if we are on the win32
platform. see http://pytest.org/latest/skipping.html @pytest.mark.xfail(condition, reason=None, run=True, raises=None, strict=False):
mark the test function as an expected failure if eval(condition) has a True val
ue. Optionally specify a reason for better reporting and run=False if you don't
even want to execute the test function. If only specific exception(s) are expect
ed, you can list them in raises, and if the test fails in other ways, it will be
reported as a true failure. See http://pytest.org/latest/skipping.html @pytest.mark.parametrize(argnames, argvalues): call a test function multiple tim
es passing in different arguments in turn. argvalues generally needs to be a lis
t of values if argnames specifies only one name or a list of tuples of values if
argnames specifies multiple names. Example: @parametrize('arg1', [,]) would l
ead to two calls of the decorated test function, one with arg1= and another wit
h arg1=.see http://pytest.org/latest/parametrize.html for more info and example
s. @pytest.mark.usefixtures(fixturename1, fixturename2, ...): mark tests as needing
all of the specified fixtures. see http://pytest.org/latest/fixture.html#usefix
tures @pytest.mark.tryfirst: mark a hook implementation function such that the plugin
machinery will try to call it first/as early as possible. @pytest.mark.trylast: mark a hook implementation function such that the plugin m
achinery will try to call it last/as late as possible.
最上面两个就是刚才写入到pytest.ini的配置了
禁用xpass
设置xfail_strict = true可以让那些标记为@pytest.mark.xfail但实际通过的测试用例被报告为失败
什么叫标记为@pytest.mark.xfail但实际通过,这个比较绕脑,看以下案例
# content of test_xpass.py
import pytest
** 作者:上海-悠悠 QQ交流群:** def test_hello():
print("hello world!")
assert @pytest.mark.xfail()
def test_yoyo1():
a = "hello"
b = "hello world"
assert a == b @pytest.mark.xfail()
def test_yoyo2():
a = "hello"
b = "hello world"
assert a != b if __name__ == "__main__":
pytest.main(["-v", "test_xpass.py"])
测试结果
collecting ... collected items test_xpass.py::test_hello PASSED [ %]
test_xpass.py::test_yoyo1 xfail [ %]
test_xpass.py::test_yoyo2 XPASS [%] =============== passed, xfailed, xpassed in 0.27 seconds ================
test_yoyo1和test_yoyo2这2个用例一个是a == b一个是a != b,两个都标记失败了,我们希望两个用例不用执行全部显示xfail。实际上最后一个却显示xpass.为了让两个都显示xfail,那就加个配置
xfail_strict = true
# pytest.ini
[pytest] markers =
webtest: Run the webtest case
hello: Run the hello case xfail_strict = true
再次运行,结果就变成
collecting ... collected items test_xpass.py::test_hello PASSED [ %]
test_xpass.py::test_yoyo1 xfail [ %]
test_xpass.py::test_yoyo2 FAILED [%] ================================== FAILURES ===================================
_________________________________ test_yoyo2 __________________________________
[XPASS(strict)]
================ failed, passed, xfailed in 0.05 seconds ================
这样标记为xpass的就被强制性变成failed的结果
配置文件如何放
一般一个工程下方一个pytest.ini文件(不要瞎jb拍脑袋乱命名,瞎jb命名是找不到的)就可以了,放到顶层文件夹下
addopts
addopts参数可以更改默认命令行选项,这个当我们在cmd输入指令去执行用例的时候,会用到,比如我想测试完生成报告,指令比较长
$ pytest -v --rerun --html=report.html --self-contained-html
每次输入这么多,不太好记住,于是可以加到pytest.ini里
# pytest.ini
[pytest] markers =
webtest: Run the webtest case
hello: Run the hello case xfail_strict = true addopts = -v --rerun --html=report.html --self-contained-html
这样我下次打开cmd,直接输入pytest,它就能默认带上这些参数了
配置文件pytest.ini的更多相关文章
- pytest十三:配置文件 pytest.ini
pytest 配置文件可以改变 pytest 的运行方式,它是一个固定的文件 pytest.ini 文件,读取配置信息,按指定的方式去运行. ini 配置文件pytest 里面有些文件是非 test ...
- Pytest系列(14)- 配置文件pytest.ini的详细使用
如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 pytest配置文件可以改变 ...
- Pytest_配置文件-pytest.ini(4)
pytest配置文件可以改变pytest的默认运行方式,它是一个固定的文件名称pytest.ini. 存放路径为项目的根目录 解决中文报错 在讲解配置文件的可用参数前,我们先解决一个高概率会遇到的问题 ...
- pytest配置文件pytest.ini
说明: pytest.ini是pytest的全局配置文件,一般放在项目的根目录下 是一个固定的文件-pytest.ini 可以改变pytest的运行方式,设置配置信息,读取后按照配置的内容去运行 py ...
- Pytest(14)pytest.ini配置文件
前言 pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行 查看pytest.ini的配置选项 pytest -h 找到以下 ...
- pytest文档18-配置文件pytest.ini
前言 pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行. ini配置文件 pytest里面有些文件是非test文件 py ...
- Pytest学习笔记12-配置文件pytest.ini
前言 pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行. 常用的配置项 marks 作用:测试用例中添加了自定义标记( ...
- pytest--mark基本使用(主要通过pytest.ini文件注册标签名,对用例进行标记分组)
1.pytest中的mark介绍 mark主要用于在测试用例/测试类中给用例打标记(只能使用已注册的标记 名),实现测试分组功能,并能和其它插件配合设置测试方法执行顺序等.如下 图,现在需要只执行红色 ...
- MySQL Cluster 配置文件(config.ini)详解
MySQL Cluster 配置文件(config.ini)详解 ################################################################### ...
随机推荐
- 【转】Linux iptables 详解
转自:https://www.cnblogs.com/qwertwwwe/p/9452370.html 最近搭一个框架需要用到iptables做映射,学习了下iptables的原理,总结下方便以后查~ ...
- Big Data(二)分布式文件系统那么多,为什么hadoop还需要一个hdfs文件系统?
提纲 - 存储模型- 架构设计- 角色功能- 元数据持久化- 安全模式- 副本放置策略- 读写流程- 安全策略 存储模型 - 文件线性按字节切割成块(block),具有offset,id - 文件与文 ...
- Monty 大厅问题(Monty Hall Problem)也称作三门问题,出自美国大型游戏节目 Let's Make a Deal。
Monty 大厅的问题陈述十分简单,但是它的答案看上去却是有悖常理.该问题不仅引起过很多争议,也经常出现在各种考试题中. Monty 大厅的游戏规则是这样的,如果你来参加这个节目,那么 (1)Mont ...
- windows中ftp下载脚本(bat+vb)
做了个ftp下载脚本: ftpdownload.bat @rem 注释:从ftp服务器每小时下载北向性能文件的脚本 @rem 用vb脚本取昨天 for /f %%a in ('cscript //no ...
- webpack 搭建React(手动搭建)
前言 最近真的都是在瞎学,看到自己不是很明白的东西,都喜欢自己手动去敲1到3遍(晚上下班的时候咯), 瞧,React 基于webpack 搭建,react 官方有一套手脚架工具,我自己也搭建过确实挺 ...
- urllib详细版
urllib是python内置的处理HTTP请求的库,主要包含以下四个模块 request 模块,是最基本的处理HTTP请求的模块. error 异常处理模块,如果出现请求错误,可以捕获这些错误,保证 ...
- POJ-2552-The Bottom of a Graph 强连通分量
链接: https://vjudge.net/problem/POJ-2553 题意: We will use the following (standard) definitions from gr ...
- 算法——求n对()有多少种输出方式?
letcode:22 Given n pairs of parentheses, write a function to generate all combinations of well-forme ...
- 【shell】sed处理多行合并
有这么一个题 文件格式 table=t1 name owner address table=t2 id text col1 comment col5 table=t3 prod_name price ...
- fabric报错:Fatal error: run() received nonzero return code 1 while executing!
今天在使用fabric远程安装rpm时,一直报:Fatal error: run() received nonzero return code 1 while executing! 这看起来也是没笔病 ...