2013-01-02 87 views

回答

0

unittest.TestCase.id()这将返回完整的Details,包括类名称,方法名称。 由此我们可以提取测试方法的名称。 可以通过检查执行测试是否存在任何异常来获得结果。 如果测试失败,那么如果sys.exc_info()返回None,则测试通过,否则测试将失败。

7

昨天我面临着同样的问题,我解决它:-)

官方手册对我来说是有帮助的: http://pytest.org/latest/plugins.html#hook-specification-and-validation

魔术这种认识,即使用特殊钩pytest。 它的钩子是“pytest_runtest_protocol” - 为给定的测试项目实现runtest_setup/call/teardown协议,包括捕获异常和调用报告钩子。

当任何测试完成时(例如启动或拆卸或测试),此钩子会被调用。

我旁边的树在我的测试目录:

./rest/ 
├── conftest.py 
├── __init__.py 
└── test_rest_author.py 

在test_rest_author.py文件我已经启动,拆卸和test_tc15功能,但我想仅显示test_tc15 FUNC结果和名称。

在conftest.py文件:(警告:创建具有相同名称的文件,它的特殊文件pytest,以了解更多信息请阅读:http://pytest.org/latest/plugins.html#conftest-py-local-per-directory-plugins

import pytest 
from _pytest.runner import runtestprotocol 

def pytest_runtest_protocol(item, nextitem): 
    reports = runtestprotocol(item, nextitem=nextitem) 
    for report in reports: 
     if report.when == 'call': 
      print '\n%s --- %s' % (item.name, report.outcome) 
    return True 

,如果你运行你的脚本,你可以看到结果和测试的名称:

$ py.test ./rest/test_rest_author.py 
====== test session starts ====== 
/test_rest_author.py::TestREST::test_tc15 PASSED 
test_tc15 --- passed 
======== 1 passed in 1.47 seconds ======= 

利润:-)

相关问题