【问题标题】:pytest overall result 'Pass' when all tests are skipped当跳过所有测试时,pytest 总体结果“通过”
【发布时间】:2015-10-10 21:36:47
【问题描述】:

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

【问题讨论】:

    标签: pytest


    【解决方案1】:

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 2013-03-28
    • 1970-01-01
    • 2019-01-16
    相关资源
    最近更新 更多