pytest.ini用法

pytest中contest.py写一些fixture,而pytest.ini写一些配置,用来改变pytest运行方式
常见场景:
1)pytest运行的时候有一些命令,如:pytest.main([‘-s’,’test_add_public.py’])
可以命令写到pytest.ini中
addopts=-v –reruns 1 –html=report.html –self-contained-html
还有形成报告的命令
运行的时候只要运行pytest就可以

2)将需要执行的testcase的目录写进来
testfile=/testcase
3)利用marks标签,marks标签是给test测试用例的别名,加进来,就会只运行这些测试用例
markers =
webtest(为用例起的别名): Run the webtest case
hello: Run the hello case
4)
禁用xpass
设置xfail_strict = true可以让那些标记为@pytest.mark.xfail但实际通过的测试用例被报告为失败

什么叫标记为@pytest.mark.xfail但实际通过,这个比较绕脑,看以下案例

import pytest

def test_hello():
print(“hello world!”)
assert 1

@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 3 items

test_xpass.py::test_hello PASSED [ 33%]
test_xpass.py::test_yoyo1 xfail [ 66%]
test_xpass.py::test_yoyo2 XPASS [100%]

=============== 1 passed, 1 xfailed, 1 xpassed in 0.27 seconds ================

test_yoyo1和test_yoyo2这2个用例一个是a == b一个是a != b,两个都标记失败了,我们希望两个用例不用执行全部显示xfail。实际上最后一个却显示xpass.为了让两个都显示xfail,那就加个配置
xfail_strict = true

[pytest]

markers =
webtest: Run the webtest case
hello: Run the hello case

xfail_strict = true
再次运行,结果就变成

collecting … collected 3 items

test_xpass.py::test_hello PASSED [ 33%]
test_xpass.py::test_yoyo1 xfail [ 66%]
test_xpass.py::test_yoyo2 FAILED [100%]

================================== FAILURES ===================================
_____ test_yoyo2 ________
[XPASS(strict)]
================ 1 failed, 1 passed, 1 xfailed in 0.05 seconds ================
这样标记为xpass的就被强制性变成failed的结果
pytest放到根目录下,且名字必须叫pytest.ini

Original: https://blog.csdn.net/qq_16782499/article/details/115696671
Author: python_搬运工
Title: pytest.ini用法

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/774575/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球