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.ini

[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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值