1. Directory structure
There are two way call fixture: 1. decorator @ pytest.mark.usefixtures ( "start"); 2. the method call as parameter directly in the fixture decorative use cases which enter def test_demo (self, start); 3 setting fixture. parameters autouse = True
2. Example pass fixture parameters
1. project with a global directory conftest.py
1 import pytest
2 @ pytest.fixture ()
3 def start ():
4 print ( " \ n全局conftest " )
2.test_case_demo / conftest.py 和 test_demo.py
conftest.py Code
. 1 Import pytest
2 @ pytest.fixture ()
. 3 DEF START_1 ():
. 4 Print ( " \ n-local test_case_demo " )
test_demo.py Code
. 1 Import pytest
2 class Test_demo (): . 3 DEF test_demo (Self, Start, START_1): # tag code . 4 Assert . 1 ==. 1 . 5 . 6 IF the __name__ == ' __main__ ' : . 7 pytest.main ([ " -s " , " -v " , " test_demo.py " ])
operation result:
3.test_case_demo_2 / conftest.py 和 test_demo_2.py
conftest.py Code
. 1 Import pytest
2 @ pytest.fixture ()
. 3 DEF start_2 ():
. 4 Print ( " \ n-local test_case_demo_2 " )
test_demo_2.py Code
. 1 Import pytest
2 class Test_demo_2 ():
. 3 DEF test_demo_2 (Self, Start, START_1): # tag code
. 4 Assert . 1 ==. 1
. 5
. 6 IF the __name__ == ' __main__ ' :
. 7 pytest.main ([ " -s " , " test_demo_2.py " , " -v " ])
The results can be seen running, start to play a global role, start_1 under test_case_demo directory no matter what its level of operation that can only be called test_case_demo below.
test_demo_2 use cases can not be cross-module call start_1 test_case_demo under the module, so test_demo_2 use cases fail
3. decorator usefixtures
fixture有 function(默认选),class,module,session,四个级别
一.只有一个.py用例文件
1)定义为@pytest.fixture(scope="function")
跟目录下的conftest.py 代码
1 import pytest
2 @pytest.fixture(scope="function")
3 def start():
4 print("\n全局conftest")
5
6 @pytest.fixture()
7 def iniv():
8 print("\n全局iniv")
test_demo.py 代码
1 import pytest
2 @pytest.mark.usefixtures("start") # 因为他们级别都是function,所以先运行iniv在运行start
3 @pytest.mark.usefixtures("iniv")
4 class Test_demo():
5 def test_demo(self):
6 assert 1==1
7 def test_demo_1(self):
8 assert 1==1
9
10 if __name__ == '__main__':
11 pytest.main(["-s","-v","test_demo.py"])
运行结果:start和iniv两个fixture都打印了
2)定义为@pytest.fixture(scope="class")
1 #跟目录下的conftest.py 代码
2 import pytest
3 @pytest.fixture(scope="class") #这个是装饰类
4 def start():
5 print("\n我是一个装饰class的start")
6
7 @pytest.fixture(scope="function") #这个是装饰用例
8 def iniv():
9 print("\n我是一个装饰function的iniv")
1 import pytest
2 @pytest.mark.usefixtures("start")
3 @pytest.mark.usefixtures("iniv")
4 class Test_demo():
5 def test_demo(self):
6 assert 1==1
7 def test_demo_1(self):
8 assert 1==1
9
10 if __name__ == '__main__':
11 pytest.main(["-s","-v","test_demo.py"])
运行结果:他会先运行start的在运行iniv的。因为start只是作用于class级别,而iniv是作用于function级别,所以start只需要执行一次,而iniv会有多少用例就运行多少次
3)定义为@pytest.fixture(scope="module")
1 #跟目录下的conftest.py 代码
2 import pytest
3 @pytest.fixture(scope="module")
4 def start():
5 print("\n我是一个装饰module的start")
6
7 @pytest.fixture(scope="function")
8 def iniv():
9 print("\n我是一个装饰function的iniv")
1 #test_demo.py代码 2 import pytest 3 @pytest.mark.usefixtures("start") 4 @pytest.mark.usefixtures("iniv") 5 class Test_demo(): 6 def test_demo(self): 7 assert 1==1 8 def test_demo_1(self): 9 assert 1==1
10 @pytest.mark.usefixtures("start") 11 @pytest.mark.usefixtures("iniv") 12 class Test_demo_1(): 13 def test_demo(self): 14 assert 1==1 15 def test_demo_1(self): 16 assert 1==1 17 18 if __name__ == '__main__': 19 pytest.main(["-s","-v","test_demo.py"])
运行结果:他会先运行start的在运行iniv的。因为start只是作用于module级别,而iniv是作用于function级别,虽然我们在test_demo.py里面装饰了两次start,但是因为它是装饰模块的,并且也只有test_demo.py这个一个模块,所以start只需要执行一次,而iniv会有多少用例就运行多少次
4)定义为@pytest.fixture(scope="session")
1 #跟目录下的conftest.py 代码
2 import pytest
3 @pytest.fixture(scope="session",autouse=True) #居然你是会话级别了,那么直接默认autouse=True就行了,不用调用就自动执行
4 def start():
5 print("\n我是一个装饰session的start")
6
7 @pytest.fixture(scope="function")
8 def iniv():
9 print("\n我是一个装饰function的iniv")
1 #test_demo.py代码
2 import pytest
3 @pytest.mark.usefixtures("iniv")
4 class Test_demo():
5 def test_demo(self):
6 assert 1==1
7 def test_demo_1(self):
8 assert 1==1
9
10 if __name__ == '__main__':
11 pytest.main(["-s","-v","test_demo.py"])
运行结果:他会先运行start的在运行iniv的。因为start只是作用于session级别,而iniv是作用于function级别,而且我们直接autouse=True,所以不用调用也会执行,而且start只需要执行一次,而iniv会有多少用例就运行多少次
二.多个.py用例文件(这里不展示代码,自己去实践吧)
多个.py用例文件,其实运行结果:
1)function级别:有多少用例就运行多少次。
2)class级别:装饰多少个类上面的,那么一个就运行多少次,例如一个.py用例里面有两个类,都装饰了class级别的,那么就运行两次,如果有两个.py用例文件都有两个类,都装饰了class级别,那么就运行四次。
3)module级别:如果有一个.py用例文件,那么就运行一次module级别,如果有两个.py用例文件,那么就运行两次。
4)session级别:不管有多少个.py用例文件,始终就运行一次
3.设置autouse=True (这种不建议,因为你不调用它,它始终就会运行)
fixture默认autouse=False
跟目录下的conftest.py 代码
1 import pytest
2 @pytest.fixture(autouse=True) #启用
3 def start():
4 print("\n全局conftest")
5
6 @pytest.fixture(autouse=False) #不启用
7 def iniv():
8 print("\n全局iniv")
1 import pytest
2 #@pytest.mark.usefixtures("start") 这里注释掉了
3 class Test_demo():
4 def test_demo(self):
5 assert 1==1
6 def test_demo_1(self):
7 assert 1==1
8 if __name__ == '__main__':
9 pytest.main(["-s","-v","test_demo.py"])
运行结果:结果还是调用了start