前面了解了pytest中的fixture的配置內容以及conftest.py中的應用,既然fixture可以代替setup和teardown,怎么在不同的場景下進行使用運行呢?比如我只想要啟動瀏覽器一次呢?如果每個用例按照前面的都加入fixture那么每條用例都會運行,其實fixture中有引數可以進行配置,配置后可以在不同的場景下進行使用,這里就要引入新的知識fixture的作用范圍,
fixture作用范圍
fixture中的scope引數可以進行配置,我們可以從原始碼中看到scope的默認引數為function,下面也介紹了scope中的其他引數:“class”,“module”,“session”
def fixture( # noqa: F811 fixture_function: Optional[_FixtureFunction] = None, *, scope: "Union[_Scope, Callable[[str, Config], _Scope]]" = "function", params: Optional[Iterable[object]] = None, autouse: bool = False, ids: Optional[ Union[ Iterable[Union[None, str, float, int, bool]], Callable[[Any], Optional[object]], ] ] = None, name: Optional[str] = None ) -> Union[FixtureFunctionMarker, _FixtureFunction]: """用于標識夾具工廠功能的裝飾器, 該裝飾器可以使用,也可以不使用引數,來定義 夾具的功能, fixture函式的名稱稍后可以被參考以導致its 在運行測驗之前呼叫:測驗模塊或類可以使用 ``pytest.mark.usefixtures(fixturename)`` marker. 測驗函式可以直接使用fixture名稱作為其中的輸入引數 從fixture函式回傳的fixture實體的情況 注入, :param scope: The scope for which this fixture is shared; one of ``"function"`` (default), ``"class"``, ``"module"``, ``"package"`` or ``"session"``.
function
引數scope默認才是就是function,主要應用單個測驗函式中,yield前面的代碼在測驗用例運行前執行,后面的代碼在測驗用例運行之后運行,未添加到的測驗函式,不進行執行
# test_01.py import pytest @pytest.fixture(scope='function') def login(): print('輸入用戶名,密碼,完成登錄!') yield print('關閉瀏覽器!') class Test_01: def test_01(self, login): print('需要用到登錄功能') print('----用例01---') def test_02(self): print('不需要登錄功能') print('----用例02---') def test_03(self, login): print('需要用到登錄功能') print('----用例03---') if __name__ == '__main__': pytest.main(['-s', 'test__01.py'])

module
scope值為“module”表示為模塊級別的fixture每個模塊之運行1次,無論模塊中多少個測驗用例,都只運行一次,
# test_01.py import pytest @pytest.fixture(scope='module') def login(): print('輸入用戶名,密碼,完成登錄!') yield print('關閉瀏覽器!') class Test_01: def test_01(self, login): print('需要用到登錄功能') print('----用例01---') def test_02(self): print('不需要登錄功能') print('----用例02---') def test_03(self, login): print('需要用到登錄功能') print('----用例03---') if __name__ == '__main__': pytest.main(['-s', 'test__01.py'])

class
scope的值為“class”表示類機會的fixture中每個測驗函式都只執行一次,無論模塊中多少個用例,都只執行一次,
# test_01.py import pytest @pytest.fixture(scope='class') def login(): print('輸入用戶名,密碼,完成登錄!') yield print('關閉瀏覽器!') class Test_01: def test_01(self, login): print('需要用到登錄功能') print('----用例01---') def test_02(self): print('不需要登錄功能') print('----用例02---') def test_03(self, login): print('需要用到登錄功能') print('----用例03---') if __name__ == '__main__': pytest.main(['-s', 'test__01.py'])

session
scope的值為“session”表示會話級別的fixture,每次會話都會只執行一次,每個添加的用例只會執行1次fixture
# test__01.py import pytest @pytest.fixture(scope='session') def login(): print('輸入用戶名,密碼,完成登錄!') yield print('關閉瀏覽器!') class Test_01: def test_01(self, login): print('需要用到登錄功能') print('----用例01---') def test_02(self): print('不需要登錄功能') print('----用例02---') def test_03(self, login): print('需要用到登錄功能') print('----用例03---') if __name__ == '__main__': pytest.main(['-s', 'test__01.py'])

執行順序
了解了fixture中scope引數的各個使用范圍,那么如果同時使用,這些執行順序是什么樣子的呢?實踐證明下
# test__01.py import pytest @pytest.fixture(scope='session') def fix_session(): print('這是屬于fixture中的會話級別') yield print('session') @pytest.fixture(scope='class') def fix_class(): print('這是屬于fixture中的類級別') yield print('session') @pytest.fixture(scope='module') def fix_module(): print('這是屬于fixture中的模塊級別') yield print('module') @pytest.fixture() def fix_function(): print('這是屬于fixture中的函式級別') yield print('function') class Test_01: def test_01(self, fix_function, fix_class,fix_module,fix_session): print('----用例01---') if __name__ == '__main__': pytest.main(['-s', 'test__01.py'])

通過上面實踐發現執行順序:session>>module>>class>>function
安靜通過簡單的例子介紹了fixture的作用范圍以及fixture的執行順序,小伙伴們可以自己動手寫一寫,畢竟實踐是檢驗真理的唯一方法,如果感覺安靜寫的不錯,可以點個關注,持續更新中~~~
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/248892.html
標籤:其他
下一篇:【簡潔明了】怎么做介面測驗?
