zoukankan      html  css  js  c++  java
  • pytest-conftest.py作用范围

    1.conftest.py解释

    conftest.py是pytest框架里面一个很重要的东西,它可以在这个文件里面编写fixture,而这个fixture的作用就相当于我们unittest框架里面的setup()和teardown(),虽然pytest框架也有setup()和teardown()但是没必要写在用例里面,直接写在conftests.py里面就好了,这样更灵活,然后pytest框架会自动去找conftest.py这个文件里面的东西。

    conftest.py
    1
    import pytest 2 @pytest.fixture() 3 def login(): 4 print("用例开始执行") 5 yield #如果有返回值,可以在yield后面直接写返回值就好了,如果有多个直接用(返回值1,返回值2) 6 print("用例执行完成")

    2.目录结构

    调用fixture有两种写法: 1.装饰器@pytest.mark.usefixtures("start") ; 2.直接在用例里面调用fixture装饰的方法当作参数输入def test_demo(self,start);3.设置fixture参数autouse=True

    3.用例传fixture参数

    1.项目跟目录下面的全局conftest.py

    1 import pytest
    2 @pytest.fixture()  
    3 def start():
    4     print("
    全局conftest")

    2.test_case_demo/conftest.py 和 test_demo.py

    conftest.py 代码

    1
    import pytest 2 @pytest.fixture() 3 def start_1(): 4 print(" 局部test_case_demo")
    test_demo.py 代码

    1
    import pytest

    2 class Test_demo(): 3 def test_demo(self,start,start_1): #标记代码 4 assert 1==1 5 6 if __name__ == '__main__': 7 pytest.main(["-s","-v","test_demo.py"])

    运行结果:

    3.test_case_demo_2/conftest.py 和 test_demo_2.py

    conftest.py 代码

    1
    import pytest 2 @pytest.fixture() 3 def start_2(): 4 print(" 局部test_case_demo_2")
    test_demo_2.py 代码
    1
    import pytest
    2 class Test_demo_2(): 3 def test_demo_2(self,start,start_1): #标记代码 4 assert 1==1 5 6 if __name__ == '__main__': 7 pytest.main(["-s","test_demo_2.py","-v"])

    运行结果可以看出,start起到全局作用,test_case_demo目录下的start_1不管它的运行级别是什么,也只能在test_case_demo下面被调用。
    test_demo_2用例不能跨模块调用test_case_demo模块下的start_1,所以test_demo_2用例会运行失败

    4.装饰器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(" 全局conftest") 5 6 @pytest.fixture() 7 def iniv(): 8 print(" 全局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("
    我是一个装饰class的start")
    6 
    7 @pytest.fixture(scope="function")   #这个是装饰用例
    8 def iniv():
    9     print("
    我是一个装饰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("
    我是一个装饰module的start")
    6 
    7 @pytest.fixture(scope="function")
    8 def iniv():
    9     print("
    我是一个装饰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就行了,不用调用就自动执行,或者不写autouse=True,直接在随便的,py用例文件里面调用以下就行了
    4 def start():
    5     print("
    我是一个装饰session的start")
    6 
    7 @pytest.fixture(scope="function")
    8 def iniv():
    9     print("
    我是一个装饰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用例文件,始终就运行一次

    5.设置autouse=True  (这种不建议,因为你不调用它,它始终就会运行)

    fixture默认autouse=False 

    
    
    跟目录下的conftest.py 代码
    1 import pytest
    2 @pytest.fixture(autouse=True)   #启用
    3 def start():
    4     print("
    全局conftest")
    5 
    6 @pytest.fixture(autouse=False)  #不启用
    7 def iniv():
    8     print("
    全局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

  • 相关阅读:
    redis排序【转载】
    程序员必读的30本书(转)
    Linux进程调度程序的具体细节(转)
    汉字转换拼音(网上找的js库)
    虚拟机配置host访问主机web
    301重定向的实现方法(转)
    浏览器关闭后,能继续执行的函数
    awk文本处理总结(转)
    DTD
    DOM编程艺术 4
  • 原文地址:https://www.cnblogs.com/hao2018/p/11351125.html
Copyright © 2011-2022 走看看