Mark標記測試用例
- 場景:只執行符合要求的某一部分用例,可以把一個web項目劃分爲多個模塊,然後指定模塊名稱執行。
- 解決:在測試用例方法上加@pytest.mark.標籤名
- 執行:-m執行自定義標記的相關用例
pytest -s test_command_param.py -m=webtest
pytest -s test_command_param.py -m apptest
pytest -s test_command_param.py -m “not ios”
import pytest
def double(a):
return a * 2
# 測試數據:整型
@pytest.mark.int
def test_double_int():
print("test double int")
assert 2 == double(1)
# 測試數據:負數
@pytest.mark.minus
def test_double_minus():
print("test double minus")
assert -2 == double(-1)
# 測試數據:浮點數
@pytest.mark.float
def test_double_float():
print("test double float")
assert 0.2 == double(0.1)
@pytest.mark.float
def test_double2_minus():
print("test double float")
assert -10.2 == double(0.2)
@pytest.mark.zero
def test_double_0():
assert 10 == double(0)
@pytest.mark.bignum
def test_double_bignum():
assert 200 == double(100)
@pytest.mark.str
def test_double_str():
assert 'aa' == double('a')
@pytest.mark.str
def test_double_str1():
assert 'a$a$' == double('a$')
跳過(Skip)及預期失敗(xFail)
這是pytest的內置標籤,可以處理一些特殊的測試用例,不能成功的測試用例等
skip:始終跳過該測試用例。採用添加裝飾器@pytest.mark.skip,或者添加跳過代碼pytest.skip(reason)等兩種方式。
skipif:遇到特定情況,跳過該測試用例。採用添加裝飾器@pytest.mark.skipif的方式。
xFail:遇到特定情況,產生一個“期望失敗”的輸出。採用添加裝飾器@pytest.mark.xfail,或者添加跳過代碼pytest.xfail(reason)等兩種方式。