2015-07-21 58 views

回答

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 

如果你想使会话失败在一定条件下,那么你可以只写一个会话范围的夹具,拆解来为你做的:

# 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)