用例執行狀態
用例執行完成后,每條用例都有自己的狀態,常見的狀態有
- passed:測試通過
- failed:斷言失敗
- error:用例本身寫的質量不行,本身代碼報錯(譬如:fixture不存在,fixture里面有報錯)
- xfail:預期失敗,加了 @pytest.mark.xfail()
error的栗子一:參數不存在
def pwd():
print("獲取用戶名")
a = "yygirl"
assert a == "yygirl123"
def test_1(pwd):
assert user == "yygirl"
為啥是error
pwd參數並不存在,所以用例執行error
error的栗子二:fixture有錯
@pytest.fixture()
def user():
print("獲取用戶名")
a = "yygirl"
assert a == "yygirl123"
return a
def test_1(user):
assert user == "yygirl"
為啥是error?
- fixture里面斷言失敗,所以fixture會報錯;
- 因為test_1調用了錯誤的fixture,所以error表示用例有問題
failed的栗子一
@pytest.fixture()
def pwd():
print("獲取密碼")
a = "yygirl"
return a
def test_2(pwd):
assert pwd == "yygirl123"
為啥是failed
因為fixture返回的變量斷言失敗
failed的栗子二
@pytest.fixture()
def pwd():
print("獲取密碼")
a = "polo"
return a
def test_2(pwd):
raise NameError
assert pwd == "polo"
為啥是failed
因為用例執行期間拋出了異常
總結
- 測試用例的代碼有異常,包括主動拋出異常或代碼有異常,都算failed
- 當測試用例調用的fixture有異常,或傳入的參數有異常的時候,都算error
- 如果一份測試報告中,error的測試用例數量越多,說明測試用例質量越差
xfail的栗子
# 斷言裝飾器,執行預期內會失敗的例子,與skip和skipif差不多一個意思
@pytest.mark.xfail(raises=ZeroDivisionError)
def test_f():
1 / 0
