接口自動化-pytest中的fixture - scope
介紹
fixture文章中介紹的比較少,同學們可以去搜索下fixture的詳解或者去看看源碼
在這之前博主都是用的unittest單元測試框架去寫的接口自動化,感覺也挺好用,但是得知pytest的fixture以及allure后,則出現了真香警告!!
先說fixture源碼中包含了幾大核心,我摘出了源碼中的一部分
def fixture( 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, )
1、scope 2、params 3、autouse 4、ids
本文章對第一條 scope進行詳細解釋,因過於詳細,非精簡版內容,適合小白進行觀看
scope:是控制fixture的作用范圍
scope中包含了4個功能
1、function
每一個函數都會調用,使用方式:在fixture函數上面增加@pytest.fixture() 括號中不用加function,不傳的話默認為function
2、class
每一個類調用一次,每個類下的方法是funtion概念 使用方式:在fixture函數當面增加@pytest.fixture(scope='class')
3、module
每一個py文件調用一次,里面的方法和類,是class和function概念 使用方式:在fixture函數當面增加@pytest.fixture(scope='module')
4、session
多個py文件調用一次,每個py文件都是module的概念 使用方式:在fixture函數當面增加@pytest.fixture(scope='session')
詳解
函數調用fixture函數的時候,是前置的
一、function
下列代碼中,可以看到,我們設置了個fixture-function函數,然后pytest中每個函數都可以調用fixture-function
import pytest @pytest.fixture() def getsql_project(): #在我們需要設置的fxture上方增加@pytest.fixture()裝飾器 project_id = 335 project_id2 = 332 print('驗證是否前置的執行') return project_id,project_id2 def test_set_project1(getsql_project): #我們要用fixture函數的時候,直接在括號中調用就好了 print('第一個id是',getsql_project[0]) def test_set_project2(getsql_project): print('第二個id是', getsql_project[1]) if __name__ == '__main__': pytest.main(["-s","test_fixture.py"])
返回結果如下
============================= test session starts ============================= collecting ... collected 2 items test_fixture_scope.py::test_set_project1 驗證是否前置的執行 PASSED [ 50%]第一個id是 335 test_fixture_scope.py::test_set_project2 驗證是否前置的執行 PASSED [100%]第二個id是 332 ============================== 2 passed in 0.08s ============================== Process finished with exit code 0
可以看到,每個方法都調用了一次fixture函數
二、class
一個類下只會觸發一次
import pytest @pytest.fixture(scope='class') def getsql_project(): #在我們需要設置的fxture上方增加@pytest.fixture()裝飾器 project_id = 335 project_id2 = 332 print('驗證是否前置的執行') return project_id,project_id2 class Test_fixture: def test_set_project1(self,getsql_project): #我們要用fixture函數的時候,直接在括號中調用就好了 print('第一個id是',getsql_project[0]) def test_set_project2(self,getsql_project): print('第二個id是', getsql_project[1]) if __name__ == '__main__': pytest.main(["-s","test_fixture.py"])
返回結果
============================= test session starts ============================= collecting ... collected 2 items test_fixture_scope.py::Test_fixture::test_set_project1 test_fixture_scope.py::Test_fixture::test_set_project2 ============================== 2 passed in 0.09s ============================== Process finished with exit code 0 驗證是否前置的執行 PASSED [ 50%]第一個id是 335 PASSED [100%]第二個id是 332
可以看到,驗證是否前置的執行,只被執行了一次,意味着我們2個方法都調用了fixture函數,實際只被執行了一次
三、module
一個py文件下只會執行一次
我們首先看下,我們有多個類,每個類都調用下fixture函數
import pytest @pytest.fixture(scope='module') def getsql_project(): #在我們需要設置的fxture上方增加@pytest.fixture()裝飾器 project_id = 335 project_id2 = 332 print('驗證是否前置的執行') return project_id,project_id2 class Test_fixture: def test_set_project1(self,getsql_project): #我們要用fixture函數的時候,直接在括號中調用就好了 print('第一個類id1',getsql_project[0]) def test_set_project2(self,getsql_project): print('第一個類id2', getsql_project[1]) if __name__ == '__main__': pytest.main(["-s","test_fixture.py"]) class Test_fixture2: def test_set_project3(self,getsql_project): #我們要用fixture函數的時候,直接在括號中調用就好了 print('第二個類id1',getsql_project[0]) def test_set_project4(self,getsql_project): print('第二個類id2', getsql_project[1]) if __name__ == '__main__': pytest.main(["-s","test_fixture.py"])
返回結果(可以看出,驗證是否前置執行 被執行了2次,因為我們定義的class,每個類被執行一次所以執行了2次)
============================= test session starts ============================= collecting ... collected 4 items test_fixture_scope.py::Test_fixture::test_set_project1 test_fixture_scope.py::Test_fixture::test_set_project2 test_fixture_scope.py::Test_fixture2::test_set_project3 驗證是否前置的執行 PASSED [ 25%]第一個類id1 335 PASSED [ 50%]第一個類id2 332 test_fixture_scope.py::Test_fixture2::test_set_project4 ============================== 4 passed in 0.09s ============================== Process finished with exit code 0 驗證是否前置的執行 PASSED [ 75%]第二個類id1 335 PASSED [100%]第二個類id2 332
重點來了
此時我們將class換成module
@pytest.fixture(scope='module')
返回結果(在整個py文件中,不論多個類調用,只被運行了一次)
============================= test session starts ============================= collecting ... collected 4 items test_fixture_scope.py::Test_fixture::test_set_project1 test_fixture_scope.py::Test_fixture::test_set_project2 test_fixture_scope.py::Test_fixture2::test_set_project3 驗證是否前置的執行 PASSED [ 25%]第一個類id1 335 PASSED [ 50%]第一個類id2 332 test_fixture_scope.py::Test_fixture2::test_set_project4 ============================== 4 passed in 0.08s ============================== Process finished with exit code 0 PASSED [ 75%]第二個類id1 335 PASSED [100%]第二個類id2 332
四、session
一般我們這種fixture都卸載目錄下的conftest.py文件下,如果有2個py文件都調用了conftest.py下的fixture函數,如果fixture是session形式,多個py可以用這一個函數返回的數據,但是不會重復調用。
新建conftest.py 文件,里面去放入我們的fixture函數
import pytest @pytest.fixture(scope='module') def getsql_project(): #在我們需要設置的fxture上方增加@pytest.fixture()裝飾器 project_id = 335 project_id2 = 332 print('驗證是否前置的執行') return project_id,project_id2
然后多個py都同時調用getsql_project 實際只被調用一次
test_fixture_scope.py::Test_fixture::test_set_project1 test_fixture_scope.py::Test_fixture::test_set_project2 test_fixture_scope.py::Test_fixture2::test_set_project3 驗證是否前置的執行 PASSED [ 25%]第一個類id1 335 PASSED [ 50%]第一個類id2 332 test_fixture_scope.py::Test_fixture2::test_set_project4 ============================== 4 passed in 0.08s ============================== Process finished with exit code 0 PASSED [ 75%]第二個類id1 335 PASSED [100%]第二個類id2 332