配置文件pytest.ini
pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行。
放在项目根目录下,不能改名。
1、markers配置
[pytest]
markers=
weibo: this is weibo
toutiao: this is toutiao
测试用例中添加了 @pytest.mark.weibo 装饰器,如果不添加marks选项的话,就会报warnings
举例:
import pytest
@pytest.mark.weibo
def test_01():
print("测试01")
if __name__ == '__main__':
pytest.main(["-s","test_py01.py"])
"""
结果:如果pytest.ini文件不添加markers
collecting ... collected 1 item
test_py01.py::test_01
======================== 1 passed, 1 warning in 0.01s =========================
Process finished with exit code 0
PASSED
结果:如果pytest.ini文件添加markers
collecting ... collected 1 item
test_py01.py::test_01 PASSED [100%]测试01
============================== 1 passed in 0.01s ==============================
"""
2、设置xfail_strict = True可以让那些标记为@pytest.mark.xfail 实际通过显示XPASS的测试用例被报告为失败
[pytest]
xfail_strict=False
import pytest
@pytest.mark.xfail
def test_01():
a = "a"
b = "b"
assert a!=b
if __name__ == '__main__':
pytest.main(["-s","test_py01.py"])
"""
结果:
collecting ... collected 1 item
test_py01.py::test_01 XPASS [100%]
============================= 1 xpassed in 0.01s ==============================
"""
[pytest]
xfail_strict=True
"""
结果:
test_py01.py::test_01 FAILED [100%]
order\test_py01.py:6 (test_01)
[XPASS(strict)]
"""
3、addopts参数可以更改默认命令行选项,这个当我们在cmd输入一堆指令去执行用例的时候,就可以用该参数代替了,省去重复性的敲命令工作
[pytest]
addopts= -v --html=report.html
import pytest
def test_01():
a = "a"
b = "b"
print(a,b)
assert a!=b
cmd命令行运行:pytest test_01.py
"""
结果:自动带上参数-v --html=report.html
collected 1 item
test_py01.py::test_01 FAILED [100%]
====================================================== FAILURES =======================================================
_______________________________________________________ test_01 _______________________________________________________
[XPASS(strict)]
------------------------------------------------ Captured stdout call -------------------------------------------------
a b
--------------------- generated html file: file://D:\pythonProject_hdc\HDC_TEST\order\report.html ---------------------
=============================================== short test summary info ===============================================
FAILED test_py01.py::test_01
================================================== 1 failed in 0.06s ==================================================
"""
4、log_cli控制台实时输出日志
log_cli=True 或False(默认),或者log_cli=1 或 0
[pytest]
lig_cli=True
import pytest
def test_01():
a = "a"
b = "b"
print(a,b)
assert a!=b
if __name__ == '__main__':
pytest.main(["-s","test_py01.py"])
"""
结果:
collecting ... collected 1 item
test_py01.py::test_01 PASSED [100%]a b
======================== 1 passed, 1 warning in 0.01s =========================
"""
网友评论