pytest整体结果' Pass'当所有测试都被跳过时

时间:2015-07-21 12:23:13

标签: pytest

当跳过所有测试时,当前pytest返回0。可以将pytest返回值配置为' fail'何时跳过所有测试?或者是否可以在执行结束时在pytest中获得总数通过/失败的测试?

1 个答案:

答案 0 :(得分:0)

可能有一个更惯用的解决方案,但到目前为止我能想到的最好的解决方案是。

修改此文档的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

如果你想在某种情况下使会话失败,那么你可以写一个会话范围的fixture-teardown来为你做这件事:

# 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)
相关问题