前言
pytest 運行全部用例的時候,在控制台會先顯示用例的運行結果(.或F), 用例全部運行完成后最后把報錯信息全部一起拋出到控制台。
這樣我們每次都需要等用例運行結束,才知道為什么報錯,不方便實時查看報錯信息。
pytest-instafail 插件可以在運行用例的時候,需實時查看用例報錯內容,這樣方便跟蹤問題。
--instafail
執行全部用例,報錯內容等用例運行完成才顯示出來
>pytest
============================= test session starts =============================
collected 11 items
test_s.py .. [ 18%]
test_t.py ... [ 45%]
test_x.py .F.F [ 81%]
test_y.py .. [100%]
================================== FAILURES ===================================
___________________________________ test_02 ___________________________________
def test_02():
a = "hello"
b = "hello world"
> assert a == b
E AssertionError: assert 'hello' == 'hello world'
E - hello
E + hello world
test_x.py:12: AssertionError
___________________________________ test_04 ___________________________________
def test_04():
a = "hello"
b = "hello world"
> assert a not in b
E AssertionError: assert 'hello' not in 'hello world'
E 'hello' is contained here:
E hello world
test_x.py:24: AssertionError
===================== 2 failed, 9 passed in 1.32 seconds ======================
當用例很多的時候,不方便我們查看具體哪個報錯對應哪條用例,加上--instafail
參數,方便實時查看報錯內容
>pytest --instafail
============================= test session starts =============================
collected 11 items
test_s.py .. [ 18%]
test_t.py ... [ 45%]
test_x.py .F
___________________________________ test_02 ___________________________________
def test_02():
a = "hello"
b = "hello world"
> assert a == b
E AssertionError: assert 'hello' == 'hello world'
E - hello
E + hello world
test_x.py:12: AssertionError
test_x.py .F
___________________________________ test_04 ___________________________________
def test_04():
a = "hello"
b = "hello world"
> assert a not in b
E AssertionError: assert 'hello' not in 'hello world'
E 'hello' is contained here:
E hello world
test_x.py:24: AssertionError
test_y.py .. [100%]
===================== 2 failed, 9 passed in 1.37 seconds ======================
結合--tb=line
參數,看起來更直觀
>pytest --instafail --tb=line
============================= test session starts =============================
collected 11 items
test_s.py .. [ 18%]
test_t.py ... [ 45%]
test_x.py .F
D:\test_x.py:12: AssertionError: assert 'hello' == 'hello world'
test_x.py .F
D:\test_x.py:24: AssertionError: assert 'hello' not in 'hello world'
test_y.py .. [100%]
===================== 2 failed, 9 passed in 1.30 seconds ======================