2012-10-26 26 views
4

我正在使用py.test報告鉤子(pytest_runtest_makereport()和pytest_report_teststatus())。我在哪裏可以找到捕獲的stdout的py.test測試通過?

當py.test測試失敗時,我可以在報告鉤子(在report.sections [])中找到捕獲的stdout數據。

當py.test測試通過時,report.sections []列表爲空。

我在哪裏可以找到通過測試的捕獲標準輸出?

謝謝。

編輯:從源(_pytest/capture.py),它看起來像這樣僅當測試不及格:

def pytest_runtest_makereport(self, __multicall__, item, call): 
    ... 
    if not rep.passed: 
     addouterr(rep, outerr) 

回答

2

原來的信息是可用item.outerr,它是兩個Unicode字符串的元組;第一個是stdout,第二個是stderr。

請注意,py.test在setup,call和teardown報告中指定了這些字符串,其中一些字符串爲空字符串。因此,爲了節省輸出,而不用空字符串覆蓋,邏輯需要爲:

stdout = item.outerr[0] 
if stdout and len (stdout): 
    whatever.stdout = stdout 

stderr = item.outerr[1] 
if stderr and len (stderr): 
    whatever.stderr = stderr 
相關問題