正文
pytest 用 @pytest.mark.usefixtures("fixtureName")装饰类,可以让执行每个case前,都执行一遍指定的fixture
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
conftest.py
import pytest
import uuid @pytest.fixture()
def declass():
print("declass:"+str(uuid.uuid4()))
return "declass"
test_forclass.py
import pytest @pytest.mark.usefixtures("declass")
class TestClass(object):
def test_case1(self):
print("test_case1:")
assert 0==0 def test_case2(self):
print("test_case2:")
assert 0 == 0
执行结果:
可以从结果中看到每个case执行前,都执行了declass这个fixture,且每次都是重新调用。 (这种使用方法,从官方给出的例子来看,应该用于数据清理或准备比较合适。)
还支持引用多个fixture
conftest.py
import pytest
import uuid @pytest.fixture()
def declass():
print("declass:"+str(uuid.uuid4()))
return "declass" @pytest.fixture()
def declass2():
print("declass2:"+str(uuid.uuid4()))
return "declass2"
test_forclass.py
@pytest.mark.usefixtures("declass","declass2")
class TestClass(object):
def test_case1(self):
print("test_case1:")
assert 0==0 def test_case2(self):
print("test_case2:")
assert 0 == 0
执行结果: