前言
pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行。
ini配置文件
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<br>
-rsxX表示pytest报告所有测试用例被跳过、预计失败、预计失败但实际被通过的原因。
使用pytest --help指令可以查看pytest.ini的设置选项
mark标记
如下案例,使用2个标签:test和add(名字可以随便起),使用mark标记功能对于以后分类测试非常有用处;
首先当前包下,新建pytest.ini文件,写入以下内容(注意:若是pytest.ini中不添加mark的标签,可以执行成功,但会报警告):
标记好之后,pytest0729的目录下使用pytest --markers查看(注意非当前pytest.ini目录,执行该命令,不会显示标记):
参考代码:
#test_answers.py
import pytest
@pytest.mark.test
def test_send_http():
print("mark web test")
def test_something_quick():
pass
def test_another():
pass
@pytest.mark.add
class TestClass:
def test_01(self):
print("hello :")
def test_02(self):
print("Hello World!")
if __name__ == "__main__":
pytest.main(["-v", "test_answers.py", "-m=add"])
cmd下运行 pytest -v -m=add
禁用xpass
设置xfail_strict=true可以让那些标记为@pytest.mark.xfail但实际通过的测试用例被报告为失败。
#test_answers.py
import pytest
def test_xixi():
print("xixi")
assert 1
@pytest.mark.xfail()
def test_t1():
a="hello"
b="hello sky"
assert a==b
@pytest.mark.xfail()
def test_t2():
a="hello"
b="hello sky"
assert a!=b
if __name__=="__main__":
pytest.main(["-s","test_answers.py"])
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。