0
當所有測試被跳過時,pytest返回0。當所有測試被跳過時,是否可以配置pytest返回值爲'失敗'?或者是否有可能在執行結束時在pytest中獲得總數/合格/不合格測試?當所有測試被跳過時pytest總體結果'通過'
當所有測試被跳過時,pytest返回0。當所有測試被跳過時,是否可以配置pytest返回值爲'失敗'?或者是否有可能在執行結束時在pytest中獲得總數/合格/不合格測試?當所有測試被跳過時pytest總體結果'通過'
有可能是一個更習慣的解決方案,但迄今爲止我能想出最好的是這樣的。
修改此文檔的example以將結果保存在某處。
# content of conftest.py
import pytest
TEST_RESULTS = []
@pytest.mark.tryfirst
def pytest_runtest_makereport(item, call, __multicall__):
rep = __multicall__.execute()
if rep.when == "call":
TEST_RESULTS.append(rep.outcome)
return rep
如果你想使會話失敗在一定條件下,那麼你可以只寫一個會話範圍的夾具,拆解來爲你做的:
# conftest.py continues...
@pytest.yield_fixture(scope="session", autouse=True)
def _skipped_checker(request):
yield
if not [tr for tr in TEST_RESULTS if tr != "skipped"]:
pytest.failed("All tests were skipped")
可惜的是故障(錯誤實際上)從這將關聯到會話中的最後一個測試用例。
如果你想改變返回值,那麼你可以寫一個鉤子:
# still conftest.py
def pytest_sessionfinish(session):
if not [tr for tr in TEST_RESULTS if tr != "skipped"]:
session.exitstatus = 10
或者只是通過調用pytest.main(),然後訪問該變量,做你自己交的會話檢查。
import pytest
return_code = pytest.main()
import conftest
if not [tr for tr in conftest.TEST_RESULTS if tr != "skipped"]:
sys.exit(10)
sys.exit(return_code)