conftest.py的用法說明

1.conftest.py與test.py放在同一個目錄下,用於實現session級別的全局唯一
2.初始化操作舉例
在conftest.py文件中,定義一個類,類變量在在初始化方法中實例化,在裏面py文件中就都可以使用該類.變量來調用該對象,保證只初始化一次
## conftest.py

 class Test1:  
        dr = None  # type:
        classss = None  # type:Ss

class Ss:
        def __init__(self):
                self.__cs = Test1.dr

        def pr1(self):
                print("方法1" + self.__cs)
        def pr2(self):
                print("方法2" + self.__cs)
        def close(self):
                print("退出")
                print("結束")

@pytest.fixture(scope='session', autouse=True)
def chishihua(request):
        Test1.dr = "ces"
        Test1.classss = Ss()
        print("所有")

        def fin():
                Test1.classss.close()  # 關閉操作
        request.addfinalizer(fin)
  1. autouse參數的差別
    不使用autouse=True,需要在其他測試方法中調用該初始化方法,使用autouse=True則不需要調用
  2. addfinalizer方法可以進行session後的收尾工作
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章