pytest的mark使用
程序员文章站
2024-02-27 17:07:45
...
@pytest.mark标签使用
1. @pytest.mark.skip(reason=’’)跳过运行
@pytest.mark.skip(reason="run failed")
def test_two():
a = "1"
assert hasattr(a,"1")
运行结果
testCase2.py::test_two SKIPPED (run failed)
2. @pytest.mark.skipif(条件,reason=’’)跳过运行
@pytest.mark.skipif(2==1,reason="flag is False")
def test_three():
a= "1 2 3"
assert "a" in a
条件不成立,所以用例进行运行,但是运行失败
[email protected](条件,reason=’’)直接运行Fail
@pytest.mark.xfail(2==1,reason="2 != 1")
def test_four():
a = "1 2 3"
assert "a" in a
[email protected]()进行传参和参数化
1、单个参数:(后边有几个参数选择,这个用例会挨个取,并且都运行一次)
import pytest
import random
@pytest.mark.parametrize('x',[(1),(2),(6)])
def test_add(x):
print(x)
assert x==random.randrange(1,7)
2、多个参数:(后边有几个参数选择,这个用例会挨个取,并且都运行一次)
import pytest
@pytest.mark.parametrize('x,y',[
(1+2,3),
(2-0,1),
(6*2,12),
(10*2,3),
("test","test")])
def test_add(x,y): #必须与上面保持一致,只能用x,y不能用其他字母
assert x==y
5.Pytest调用fixture之@pytest.mark.usefixtures()、叠加usefixtures、@pytest.fixture(autouse=True)用法详解
参考博客:https://blog.csdn.net/qq_36502272/article/details/100882727
@pytest.mark.usefixtures() 不如直接使用fixture标签
上一篇: C/C++中对数据的取整处理