scope有4個作用范圍:function、class、module、session
function:每個函數或方法都會調用
class:每個類只調用1次
module:每個模塊只調用1次
session:多個模塊調用1次,通常寫在conftest中
scope=function
""" scope=function 執行順序:setup_module ->fixture->test ->fixture->test .. """ @pytest.fixture() def login(): print('登陸') @pytest.fixture(scope='function') def logout(): print('登出') def setup_module(): print(f'setup') def test_s1(login): print('s1') def test_s2(logout): print('s2') def test_s3(): print('s3') if __name__ == '__main__': pytest.main(['-q','s001_function.py'])
運行結果:
collecting ... collected 3 items s001_function.py::test_s1 setup 登陸 PASSED [ 33%]s1 s001_function.py::test_s2 登出 PASSED [ 66%]s2 s001_function.py::test_s3 PASSED [100%]s3
scope=function使用在類中

import pytest """ scope=fuction 運行順序:setup_method -> fixture -> test -> setup_method -> fixture -> test """ @pytest.fixture() def login(): print('登陸') @pytest.fixture(scope='function') def logout(): print('登出') class Test1: def setup_method(self): print('setup_method') def test_s1(self,login): print('s1') def test_s2(self,logout): print('s2') if __name__ == '__main__': pytest.main(['-q','s002_function_運用在class中.py'])
運行結果
collecting ... collected 2 items s002_function_運用在class中.py::Test1::test_s1 setup_method 登陸 PASSED [ 50%]s1 s002_function_運用在class中.py::Test1::test_s2 setup_method 登出 PASSED [100%]s2 ============================== 2 passed in 0.20s ==============================
scope=class
import pytest """ scope=class 1、若class中的每個test用例都調用了login,只在class所有用例執行前運行一次 2、運行順序 setup_class -> fixture函數(整個類的運行過程中只運行一次,測試用例執行前)-> 測試用例 """ @pytest.fixture(scope='class') def login(): print('登陸') class Test2: def setup_class(self): print('setup') def test_s1(self,login): print('s1') def test_s2(self,login): print('s2') def test_s3(self): print('s3') if __name__ == '__main__': pytest.main(['-q','s003_class.py'])
運行結果:
collecting ... collected 3 items
s003_class.py::Test2::test_s1 setup
登陸
PASSED [ 33%]s1
s003_class.py::Test2::test_s2 PASSED [ 66%]s2
s003_class.py::Test2::test_s3 PASSED [100%]s3
============================== 3 passed in 0.22s ==============================
scope=module
import pytest """ scope=module:整個.py模塊中login只執行一次 執行順序:setup_module -> fixture -> tests """ @pytest.fixture(scope='module') def login(): print('登陸') def setup_module(): print('setup_module') def test_s1(login): print('s1') class Test1: def test_s2(self,login): print('s2') if __name__ == '__main__': pytest.main(['-q', 's004_module.py'])
運行結果:
collecting ... collected 2 items s004_module.py::test_s1 setup_module 登陸 PASSED [ 50%]s1 s004_module.py::Test1::test_s2 PASSED [100%]s2 ============================== 2 passed in 0.19s ==============================
scope=session
scope=session,可以跨多個.py文件,若多個py模塊中的用例都調用了 fixture,只會運行一次(在調用fixture的用例開始時運行一次)
fixture(scope='session') 函數寫在 conftest.py 文件中
conftest.py 文件文件名固定,放在工程根目錄則是全局使用,放在某個package下,則只針對該package下的py文件有效
conftest.py
import pytest @pytest.fixture(scope='session') def gblogin(): print('gb登陸--session')
如上,test_001 和 test_005中分別調用了gblogin
test_001

import pytest """ :arg scope: scope 有四個級別參數 "function" (默認), "class", "module" or "session". scope=function 執行順序:setup_module ->fixture->test ->fixture->test .. """ @pytest.fixture() def login(): print('登陸') @pytest.fixture(scope='function') def logout(): print('登出') def setup_module(): print(f'setup') def test_s1(login): print('s1') def test_s2(logout): print('s2') def test_s3(gblogin): print('s3')
test_005

import pytest def test1(gblogin): print('s1')
執行所有測試用例:
E:\f004_fixtrue\f02_scope>pytest -s
運行結果:
collected 11 items
test_001_function.py setup
登陸
s1
.登出
s2
.gb登陸--session
s3
.
test_002_function_運用在class中.py setup_method
登陸
s1
.setup_method
登出
s2
.
test_003_class.py setup
登陸
s1
.s2
.s3
.
test_004_module.py setup_module
登陸
s1
.s2
.
test_005_session.py s1
.
================================================= 11 passed in 0.53s ==================================================