2013-01-02 88 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 ======= 

利潤:-)

相關問題