關(guān)于自動化測試框架pytest的Fixture固件
什么是固件
Fixture 翻譯成中文即是固件的意思。它其實就是一些函數(shù),會在執(zhí)行測試方法/測試函數(shù)之前(或之后)加載運行它們,常見的如接口用例在請求接口前數(shù)據(jù)庫的初始連接,和請求之后關(guān)閉數(shù)據(jù)庫的操作。
我們之前在APP UI自動化系列中已經(jīng)介紹過 unittest 的相關(guān)測試固件,如setup、teardown等。而 pytest 中提供了功能更加豐富的Fixture,用于實現(xiàn)setup、teardown功能。
定義方式
使用@pytest.fixture()進行定義,簡單示例如下:
import pytest
@pytest.fixture()
def before():
print("連接數(shù)據(jù)庫")調(diào)用方式
調(diào)用單個fixture函數(shù)
- 方式一,使用fixture函數(shù)名作為參數(shù)
import pytest
@pytest.fixture()
def before():
print("連接數(shù)據(jù)庫")
# 調(diào)用before
def test_01(before):
print("執(zhí)行test_01")- 方式二,使用
@pytest.mark.usefixtures('fixture函數(shù)名')裝飾器
import pytest
@pytest.fixture()
def before():
print("連接數(shù)據(jù)庫")
# 調(diào)用before
@pytest.mark.usefixtures('before')
def test_01():
print("執(zhí)行test_01")- 方式三,使用
autouse參數(shù)自動執(zhí)行fixture函數(shù)
import pytest
# fixture函數(shù)定義的時候使用autouse參數(shù),作用域范圍內(nèi)的測試用例會自動調(diào)用該fixture函數(shù)
@pytest.fixture(autouse=True)
def before():
print("連接數(shù)據(jù)庫")
# 自動調(diào)用before
def test_01():
print("執(zhí)行test_01")三種方式調(diào)用后的結(jié)果都如下:

我們可以看到,先執(zhí)行了fixture函數(shù),再執(zhí)行測試函數(shù)。
調(diào)用多個fixture函數(shù)
import pytest
@pytest.fixture()
def before():
print("連接數(shù)據(jù)庫")
@pytest.fixture()
def before_s():
print("初始化數(shù)據(jù)")
def test_01(before, before_s):
print("執(zhí)行test_01")調(diào)用多個 fixture 函數(shù)時,由前至后依次執(zhí)行,所以test_01()調(diào)用時先執(zhí)行before,再執(zhí)行before_s。
對fixture函數(shù)重命名
定義fixture函數(shù)時,可以利用name參數(shù)進行重命名,方便用于調(diào)用,示例如下:
import pytest
@pytest.fixture(name='db')
def connect_order_db():
print("連接數(shù)據(jù)庫")
def test_01(db):
print("執(zhí)行test_01")使用fixture傳遞測試數(shù)據(jù)
在執(zhí)行完fixture函數(shù)后,有時需要將該fixture中得到到某些數(shù)據(jù)傳遞給測試函數(shù)/測試方法,用于后續(xù)的執(zhí)行。
fixture中提供普通傳遞和參數(shù)化傳遞兩種數(shù)據(jù)傳遞方式。
普通傳遞
示例如下:
import pytest
@pytest.fixture()
def before():
print("連接數(shù)據(jù)庫")
return "連接成功!"
def test_01(before):
print("執(zhí)行test_01")
assert before == "連接成功!"注意,如果自定義的fixture函數(shù)有返回值,需要使用上面說的方式一調(diào)用才能獲取fixture函數(shù)的返回值并傳入測試函數(shù)中,方式二就無法獲取返回值。
參數(shù)化傳遞
對fixture函數(shù)進行參數(shù)化時,需要使用參數(shù)params,并且需要傳入?yún)?shù)request,簡單示例如下:
import pytest
test_params = [1, 2, 0]
@pytest.fixture(params=test_params)
def before(request):
result = request.param
return result
def test_02(before):
print("執(zhí)行test_02")
assert before
if __name__ == '__main__':
pytest.main()執(zhí)行結(jié)果:

可以看到,因為所調(diào)用的fixture函數(shù)進行了參數(shù)化,雖然只有一個測試函數(shù)但執(zhí)行了3次。
conftest.py
上面我們舉的例子都是把fixture函數(shù)放在測試用例模塊里面,但如果很多測試模塊需要引用同一個fixture函數(shù)怎么辦,這是時候就需要把它放在命名為conftest的模塊里,這樣同級或以下目錄中的測試用例便能調(diào)用這些自定義的fixture函數(shù)。
例如,有如下目錄:
├─testcase │ │ │ ├─test_module_01 │ │ test_case_1.py │ │ test_case_2.py │ │ │ ├─test_module_02 │ │ test_case_3.py
test_module_01 中的test_case_1.py與test_case_2.py都需要調(diào)用同一個 fixture 函數(shù),那么我們只需要在 test_module_01 中新建conftest.py并編寫這個fixture函數(shù)即可,示例如下:
├─testcase │ │ │ ├─test_module_01 │ │ conftest.py │ │ test_case_1.py │ │ test_case_2.py │ │ │ ├─test_module_02 │ │ test_case_3.py
conftest.py:
import pytest
@pytest.fixture(autouse=True)
def before():
print("連接數(shù)據(jù)庫")test_case_1.py:
def test_01():
print("執(zhí)行test_01")test_case_2.py:
def test_02():
print("執(zhí)行test_02")這樣,執(zhí)行這兩個模塊的測試用例時會自動先去調(diào)用conftest.py中的before()函數(shù)。
假設(shè) test_module_02 中的 test_case_3.py 也需要調(diào)用這個before()函數(shù),那么這個時候我們就需要在上一層即 testcase 中新建conftest.py并編寫這個before()函數(shù),才能在 test_case_3.py 中調(diào)用,如下:
├─testcase │ │ conftest.py │ │ │ ├─test_module_01 │ │ conftest.py │ │ test_case_1.py │ │ test_case_2.py │ │ │ ├─test_module_02 │ │ test_case_3.py
conftest.py只作用于同級或以下目錄中的測試模塊,且需要注意,當以下層級中存在了另一個conftest.py,那么以下層級將由另一個conftest.py文件接管。
作用域
pytest 的 fixture 作用域分session、module、class、function四個級別。在定義 fixture 函數(shù)的時候通過scope參數(shù)指定作用范圍,默認為function。
session,每次會話執(zhí)行一次module,每個測試模塊執(zhí)行一次class,每個測試類執(zhí)行一次function,每個測試方法執(zhí)行一次
注意,對于單獨定義的測試函數(shù),class、function 都會起作用,可以從下列示例中看出來。
測試目錄結(jié)構(gòu)如下:
├─apiAutoTest │ │ run.py │ │ │ ├─testcase │ │ │ conftest.py │ │ │ │ │ ├─test_module_02 │ │ │ │ conftest.py │ │ │ │ test_case_3.py │ │ │ │ test_case_4.py
其中conftest.py代碼如下:
import pytest
@pytest.fixture(scope="session", autouse=True)
def session_fixture():
print("這是一個作用于session的fixture")
@pytest.fixture(scope="module", autouse=True)
def module_fixture():
print("這是一個作用于module的fixture")
@pytest.fixture(scope="class", autouse=True)
def class_fixture():
print("這是一個作用于class的fixture")
@pytest.fixture(scope="function", autouse=True)
def function_fixture():
print("這是一個作用于function的fixture")test_case_3.py代碼如下:
import pytest
class TestOrder:
def test_a(self):
print("test_a")
def test_b(self):
print("test_b")
def test_c():
print("test_c")test_case_4.py代碼如下:
def test_e():
print("test_e")run.py代碼如下:
import pytest
if __name__ == '__main__':
pytest.main(["-s"])運行run.py,結(jié)果如下:
collected 4 items testcase\test_module_02\test_case_3.py 這是一個作用于session的fixture 這是一個作用于module的fixture 這是一個作用于class的fixture 這是一個作用于function的fixture test_a .這是一個作用于function的fixture test_b .這是一個作用于class的fixture 這是一個作用于function的fixture test_c . testcase\test_module_02\test_case_4.py 這是一個作用于module的fixture 這是一個作用于class的fixture 這是一個作用于function的fixture test_e . ============================== 4 passed in 0.04s ==============================
從結(jié)果可以看出來:
- 作用于
session的fixture函數(shù)只在所有測試用例執(zhí)行之前調(diào)用了一次 - 作用于
module的fixture函數(shù)在每個測試模塊執(zhí)行之前調(diào)用了一次 - 作用于
class的fixture函數(shù)在每個測試類執(zhí)行之前調(diào)用了一次 - 作用于
function的fixture函數(shù)在每個測試方法/測試函數(shù)執(zhí)行之前調(diào)用了一次
注意,在定義的測試函數(shù)(如test_c()、test_e())執(zhí)行之前也會調(diào)用scope=class的fixture函數(shù)。
總結(jié)
與 unittest 框架比較,pytest 中的Fixture更加豐富,可擴展性更高。
Fixture還有很多更加優(yōu)雅的用法用于自動化測試項目中,本文只是以最簡單的示例進行說明。
到此這篇關(guān)于關(guān)于自動化測試框架pytest的Fixture固件的文章就介紹到這了,更多相關(guān)自動化測試Fixture固件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解如何使用python實現(xiàn)猜數(shù)字游戲
“猜數(shù)字”游戲是一款簡單而有趣的小游戲,玩家需要在給定的范圍內(nèi)猜出一個由計算機隨機生成的數(shù)字,本文將使用Python語言來實現(xiàn)這款游戲,并詳細介紹其實現(xiàn)過程,文中有詳細的代碼示例供大家參考,需要的朋友可以參考下2024-04-04
django inspectdb 操作已有數(shù)據(jù)庫數(shù)據(jù)的使用步驟
這篇文章主要介紹了django inspectdb 操作已有數(shù)據(jù)庫數(shù)據(jù)的使用步驟,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
selenium+python實現(xiàn)基本自動化測試的示例代碼
這篇文章主要介紹了selenium+python實現(xiàn)基本自動化測試的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01
Python如何基于selenium實現(xiàn)自動登錄博客園
這篇文章主要介紹了Python如何基于selenium實現(xiàn)自動登錄博客園,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-12-12

