Pytest-ordering使用者自定義用例執行順序,skip/skipif裝飾器用法詳解(三)
Pytest-ordering使用者自定義用例執行順序以及skip/skipif裝飾器的用法
Pytest提供了很強大的第三方庫來實現執行順序控制Pytest-ordering,用例設計之間應該是可以相互獨立執行的,
沒有一定的前後依賴關係的,如果我們真的有前後依賴,想指定用例的先後順序,可以用到pytest-ordering外掛解
決這個問題!
Pytest-ordering 外掛下載:
pip install Pytest-ordering -i https://pypi.douban.com/simple
前言:
Pytest用例執行順序介紹:
1.瞭解過Unittest單元測試框架的小夥伴都知道Unittest單元測試框架的執行順序師按照ASCII的順序進行執行
2.Pytest單元測試框架是按照預設是按照程式碼的先後順序進行執行
3.pytest改變預設的執行順序可以使用:mark裝飾器進行標記
示例:
#執行全部case
class Test_Case: def test01(self): print(" case 01") def test02(self): print(" case 02") def test03(self): print(" case 03") def test05(self): print(" case 04")def test06(self): print(" case 05")
if __name__ == "mian":
pytest.main(['./case_api/test_case01.py',"-vs"])
響應示例:
case_api/test_case01.py::Test_Case::test01 case 01
PASSED
case_api/test_case01.py::Test_Case::test02 case 02
PASSED
case_api/test_case01.py::Test_Case::test03 case 03
PASSED
case_api/test_case01.py::Test_Case::test05 case 04
PASSED
case_api/test_case01.py::Test_Case::test06 case 05
PASSED
============================== 5 passed in 0.09s =======================
結果:以上可以看見,pytest 是按照預設程式碼順序進行執行case
使用mark裝飾器進行修飾case執行的順序:
示例:
這裡我使用 mark裝飾器修飾了case的執行順序 進行倒序,我們看下實際的執行結果
import pytest class Test_Case: @pytest.mark.run(order=5) def test01(self): print(" case 01") @pytest.mark.run(order=4) def test02(self): print(" case 02") @pytest.mark.run(order=3) def test03(self): print(" case 03") @pytest.mark.run(order=2) def test05(self): print(" case 04") @pytest.mark.run(order=1) def test06(self): print(" case 05")
實際執行結果:
大家可以看到實際的輸出結果順序,是按照我的order順序進行執行的
case_api/test_case01.py::Test_Case::test06 case 05 PASSED case_api/test_case01.py::Test_Case::test05 case 04 PASSED case_api/test_case01.py::Test_Case::test03 case 03 PASSED case_api/test_case01.py::Test_Case::test02 case 02 PASSED case_api/test_case01.py::Test_Case::test01 case 01 PASSED ============================== 5 passed in 0.07s ========================
pytest當中如果遇到不想執行的case怎麼辦呢? pyest也提供了跳過執行case的方法具體我們來看看
1)skip裝飾器:直接跳過當前case
2)skipif裝飾器: 條件成立跳過當前case
示例:skip裝飾器進行標記case跳過case執行
import pytest
class Test_Case:
age = 18
@pytest.mark.run(order=5)
@pytest.mark.skip(reason="功能暫時不通過")
def test01(self):
print(" case 01")
@pytest.mark.run(order=4)
def test02(self):
print(" case 02")
@pytest.mark.run(order=3)
def test03(self):
print(" case 03")
@pytest.mark.run(order=2)
def test05(self):
print(" case 04")
@pytest.mark.run(order=1)
def test06(self):
print(" case 05")
#輸出結果
case_api/test_case01.py::Test_Case::test06 case 05
PASSED
case_api/test_case01.py::Test_Case::test05 case 04
PASSED
case_api/test_case01.py::Test_Case::test03 case 03
PASSED
case_api/test_case01.py::Test_Case::test02 case 02
PASSED
case_api/test_case01.py::Test_Case::test01 SKIPPED (功能暫時不通過)
======================== 4 passed, 1 skipped in 0.11s =========================
輸出結果當中大家可以清晰的看到,執行通過了4條cass 1條casa 沒有執行
case_api/test_case01.py::Test_Case::test01 SKIPPED (功能暫時不通過)
示例二:skipif 條件判斷跳過case,條件成立時跳過當前的case
import pytest class Test_Case: @pytest.mark.run(order=5) @pytest.mark.skipif(1 == 1,reason="滿足條件跳過用例") # def test01(self): print(" case 01") @pytest.mark.run(order=4) @pytest.mark.skipif(1 == 2, reason="不滿足條件不跳過用例") def test02(self): print(" case 02") @pytest.mark.run(order=3) def test03(self): print(" case 03") @pytest.mark.run(order=2) def test05(self): print(" case 04") @pytest.mark.run(order=1) def test06(self): print(" case 05") case_api/test_case01.py::Test_Case::test06 case 05 PASSED case_api/test_case01.py::Test_Case::test05 case 04 PASSED case_api/test_case01.py::Test_Case::test03 case 03 PASSED case_api/test_case01.py::Test_Case::test02 case 02 PASSED case_api/test_case01.py::Test_Case::test01 SKIPPED (滿足條件跳過用例) ======================== 4 passed, 1 skipped in 0.08s =========================
輸出結果:大家可以清楚的看到skipif 條件成立的用例進行標記跳過了,不滿足的條件的case
執行成功