文 | 遠山近海
手把手教你pytest測試框架(1)-初識框架
手把手教你pytest測試框架(2)-參數講解
手把手教你pytest測試框架(3)-用例失敗重跑
手把手教你pytest測試框架(4)-前置和後置
手把手教你pytest測試框架(5)-mark標記裝飾器
注意:
以上名稱為個人命名,並非官方定義
調用是可以疊加調的,即你可以設置多個fixture函數,在一個函數裡同時調用這多個fixtrue函數
fixture函數的命名最好不要加test_,雖然語法上允許,但為了和用例區分,最好不加
參數調用法中並沒有類參數總調用法,而裝飾器調用法中則有一個類裝飾器總調用法,所以這兩大類裡的方法並不完全是一一對應的
fixture函數的調用方法詳細說明參數調用法函數參數調用法1. 說明2. 單一調用# coding=utf-8import pytest
@pytest.fixture()def fix(): print("執行fix")
def test_case1(fix): print("執行用例1")
def test_case2(fix): print("執行用例2")
if __name__ == '__main__': pass可見,用例1和用例2在執行的時候,首先先調用了test_fix()。3. 疊加調用
# coding=utf-8import pytest
@pytest.fixture()def fix1(): print("執行fix1")
@pytest.fixture()def fix2(): print("執行fix2")
def test_case1(fix1,fix2): print("執行用例1")
def test_case2(fix1,fix2): print("執行用例2")
if __name__ == '__main__': pass可見,疊加調用後,fixtrue函數被按照參數的書寫順序調用。類參數分別調用法1. 說明在類的每個函數裡分別通過參數的形式調用fixture函數2. 單一調用# coding=utf-8import pytest
@pytest.fixture()def fix1(): print("執行fix1")
@pytest.fixture()def fix2(): print("執行fix2")
@pytest.fixture()def fix3(): print("執行fix3")
class Test_case:
def test_case1(self,fix1): print("執行用例1")
def test_case2(self,fix2): print("執行用例2")
def test_case3(self,fix3): print("執行用例3")
if __name__ == '__main__': pytest.main(["-s", "test_study.py"])test_study.py 執行fix1
執行用例1
.執行fix2
執行用例2
.執行fix3
執行用例3
3. 疊加調用# coding=utf-8import pytest
@pytest.fixture()def fix1(): print("執行fix1")
@pytest.fixture()def fix2(): print("執行fix2")
@pytest.fixture()def fix3(): print("執行fix3")
class Test_case: def test_case1(self,fix1,fix2,fix3): print("執行用例1")
if __name__ == '__main__': pytest.main(["-s", "test_study.py"])test_study.py 執行fix1
執行fix2
執行fix3
執行用例1
(未完待續)
👆單擊連結直達、長按諮詢吳老師或公眾號內回復「2」
無論上課或自學,
你首先需要準備:
每天 2 小時+的學習時間,
每天堅持寫代碼的習慣!
有投入才有產出,
10k+的漲幅需要 1 年以上的努力!
祝你成功!
光榮之路出品
測試大佬和小白的故事
招聘QQ群:203715128