pytest 用 @pytest.mark.usefixtures("fixtureName")装饰类,可以让执行每个case前,都执行一遍指定的fixture
conftest.py
1 import pytest 2 import uuid 3 4 @pytest.fixture() 5 def declass(): 6 print("declass:"+str(uuid.uuid4())) 7 return "declass"
test_forclass.py
1 import pytest 2 3 4 @pytest.mark.usefixtures("declass") 5 class TestClass(object): 6 def test_case1(self): 7 print("test_case1:") 8 assert 0==0 9 10 11 def test_case2(self): 12 print("test_case2:") 13 assert 0 == 0
执行结果:
可以从结果中看到每个case执行前,都执行了declass这个fixture,且每次都是重新调用。 (这种使用方法,从官方给出的例子来看,应该用于数据清理或准备比较合适。)
还支持引用多个fixture
conftest.py
1 import pytest 2 import uuid 3 4 @pytest.fixture() 5 def declass(): 6 print("declass:"+str(uuid.uuid4())) 7 return "declass" 8 9 @pytest.fixture() 10 def declass2(): 11 print("declass2:"+str(uuid.uuid4())) 12 return "declass2"
test_forclass.py
1 @pytest.mark.usefixtures("declass","declass2") 2 class TestClass(object): 3 def test_case1(self): 4 print("test_case1:") 5 assert 0==0 6 7 8 def test_case2(self): 9 print("test_case2:") 10 assert 0 == 0
执行结果:
本文来自博客园,作者:月色深潭,交流群:733423266,转载请注明原文链接:https://www.cnblogs.com/moonpool/p/11352207.html