該插件提供了兩個命令行選項來重新運行上次 pytest 調(diào)用的失?。?/p>
--lf
?, ?--last-failed
? - 只重新運行失敗。--ff
?, ?--failed-first
? - 先運行失敗,然后運行其余測試。對于清理(通常不需要), ?--cache-clear
? 選項允許在測試運行之前刪除所有跨會話緩存內(nèi)容。
其他插件可以訪問 ?config.cache
? 對象以在 pytest 調(diào)用之間設(shè)置/獲取 ?json
可編碼值。
首先,我們創(chuàng)建50個測試調(diào)用,其中只有2個失敗:
# content of test_50.py
import pytest
@pytest.mark.parametrize("i", range(50))
def test_num(i):
if i in (17, 25):
pytest.fail("bad luck")
如果你第一次運行這個,你會看到兩個失敗:
$ pytest -q
.................F.......F........................ [100%]
================================= FAILURES =================================
_______________________________ test_num[17] _______________________________
i = 17
@pytest.mark.parametrize("i", range(50))
def test_num(i):
if i in (17, 25):
> pytest.fail("bad luck")
E Failed: bad luck
test_50.py:7: Failed
_______________________________ test_num[25] _______________________________
i = 25
@pytest.mark.parametrize("i", range(50))
def test_num(i):
if i in (17, 25):
> pytest.fail("bad luck")
E Failed: bad luck
test_50.py:7: Failed
========================= short test summary info ==========================
FAILED test_50.py::test_num[17] - Failed: bad luck
FAILED test_50.py::test_num[25] - Failed: bad luck
2 failed, 48 passed in 0.12s
然后如果你用 ?--lf
? 運行它:
$ pytest --lf
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-7.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
collected 2 items
run-last-failure: rerun previous 2 failures
test_50.py FF [100%]
================================= FAILURES =================================
_______________________________ test_num[17] _______________________________
i = 17
@pytest.mark.parametrize("i", range(50))
def test_num(i):
if i in (17, 25):
> pytest.fail("bad luck")
E Failed: bad luck
test_50.py:7: Failed
_______________________________ test_num[25] _______________________________
i = 25
@pytest.mark.parametrize("i", range(50))
def test_num(i):
if i in (17, 25):
> pytest.fail("bad luck")
E Failed: bad luck
test_50.py:7: Failed
========================= short test summary info ==========================
FAILED test_50.py::test_num[17] - Failed: bad luck
FAILED test_50.py::test_num[25] - Failed: bad luck
============================ 2 failed in 0.12s =============================
您只運行了上次運行的兩個失敗的測試,而 48 個通過的測試還沒有運行。
現(xiàn)在,如果您使用 ?--ff
? 選項運行,所有測試都將運行,但之前的第一個失敗將首先執(zhí)行:
$ pytest --ff
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-7.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
collected 50 items
run-last-failure: rerun previous 2 failures first
test_50.py FF................................................ [100%]
================================= FAILURES =================================
_______________________________ test_num[17] _______________________________
i = 17
@pytest.mark.parametrize("i", range(50))
def test_num(i):
if i in (17, 25):
> pytest.fail("bad luck")
E Failed: bad luck
test_50.py:7: Failed
_______________________________ test_num[25] _______________________________
i = 25
@pytest.mark.parametrize("i", range(50))
def test_num(i):
if i in (17, 25):
> pytest.fail("bad luck")
E Failed: bad luck
test_50.py:7: Failed
========================= short test summary info ==========================
FAILED test_50.py::test_num[17] - Failed: bad luck
FAILED test_50.py::test_num[25] - Failed: bad luck
======================= 2 failed, 48 passed in 0.12s =======================
新的 ?--nf
?,?--new-first
? 選項:首先運行新測試,然后是其余測試,在這兩種情況下,測試也按文件修改時間排序,最近的文件首先出現(xiàn)。
如果在上次運行中沒有測試失敗,或者沒有找到緩存的 lastfailed 數(shù)據(jù),則可以使用 --last-failed-no-failures 選項將 pytest 配置為運行所有測試或不運行測試,該選項采用以下選項之一 :
pytest --last-failed --last-failed-no-failures all # run all tests (default behavior)
pytest --last-failed --last-failed-no-failures none # run no tests and exit
插件或 ?conftest.py
? 支持代碼可以使用 pytest 配置對象獲取緩存值。 這是一個基本示例插件,它實現(xiàn)了一個在 pytest 調(diào)用中重用先前創(chuàng)建的狀態(tài)的?fixture
?:
# content of test_caching.py
import pytest
import time
def expensive_computation():
print("running expensive computation...")
@pytest.fixture
def mydata(request):
val = request.config.cache.get("example/value", None)
if val is None:
expensive_computation()
val = 42
request.config.cache.set("example/value", val)
return val
def test_function(mydata):
assert mydata == 23
如果你是第一次運行這個命令,你可以看到 ?print
語句:
$ pytest -q
F [100%]
================================= FAILURES =================================
______________________________ test_function _______________________________
mydata = 42
def test_function(mydata):
> assert mydata == 23
E assert 42 == 23
test_caching.py:20: AssertionError
-------------------------- Captured stdout setup ---------------------------
running expensive computation...
========================= short test summary info ==========================
FAILED test_caching.py::test_function - assert 42 == 23
1 failed in 0.12s
如果您再次運行它,將從緩存中檢索該值,并且不會打印任何內(nèi)容:
$ pytest -q
F [100%]
================================= FAILURES =================================
______________________________ test_function _______________________________
mydata = 42
def test_function(mydata):
> assert mydata == 23
E assert 42 == 23
test_caching.py:20: AssertionError
========================= short test summary info ==========================
FAILED test_caching.py::test_function - assert 42 == 23
1 failed in 0.12s
您始終可以使用 ?--cache-show
? 命令行選項查看緩存的內(nèi)容:
$ pytest --cache-show
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-7.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
cachedir: /home/sweet/project/.pytest_cache
--------------------------- cache values for '*' ---------------------------
cache/lastfailed contains:
{'test_caching.py::test_function': True}
cache/nodeids contains:
['test_caching.py::test_function']
cache/stepwise contains:
[]
example/value contains:
42
========================== no tests ran in 0.12s ===========================
?--cache-show
? 采用可選參數(shù)來指定用于過濾的全局模式:
$ pytest --cache-show example/*
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-7.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
cachedir: /home/sweet/project/.pytest_cache
----------------------- cache values for 'example/*' -----------------------
example/value contains:
42
========================== no tests ran in 0.12s ===========================
您可以通過添加 ?--cache-clear
? 選項來指示 pytest 清除所有緩存文件和值,如下所示:
pytest --cache-clear
對于來自持續(xù)集成服務(wù)器的調(diào)用,建議這樣做,因為隔離和正確性比速度更重要。
作為 ?--lf -x
? 的替代方案,特別是對于您預(yù)計大部分測試套件將失敗的情況,?--sw
?, ?--stepwise
? 允許您一次修復(fù)一個。 測試套件將一直運行到第一次失敗,然后停止。 在下一次調(diào)用時,測試將從上一個失敗的測試繼續(xù),然后運行直到下一個失敗的測試。 您可以使用 ?--stepwise-skip
? 選項忽略一個失敗的測試并在第二個失敗的測試上停止測試執(zhí)行。 如果您陷入失敗的測試并且只想在以后忽略它,這很有用。 提供 ?--stepwise-skip
? 也將隱式啟用 ?--stepwise
?
更多建議: