2016-11-12 141 views
1

我想生成一個自定義的pyunit測試套件執行報告,但用'沒有分支'錯誤打。生成自定義pyunit報告

import json 
import unittest 
import sys 

class MyTestResult(unittest._TextTestResult): 
    def addSuccess(self, test): 
     TestResult.addSuccess(self, test) 
    def addError(self, test, err): 
     TestResult.addError(self, test, err) 
    def addFailure(self, test, err): 
     TestResult.addFailure(self, test, err) 

class MyTestRunner(unittest.TextTestRunner): 
    def _makeResult(self, verbosity): 
     return MyTestResult(self.stream, self.descriptions, verbosity) 

class TestServer(unittest.TestCase): 
    def testFunction1(self): 
     res = True 
     self.assertTrue(res, "test case failed") 
    def testFunction2(self): 
     res = 5 
     self.assertEqual(res, 5) 
    def testFunction3(self): 
     res = True 
     self.assertEqual(res, True, 'test case failed') 
    def testFunction4(self): 
     res = False 
     self.assertEqual(res, True, 'test case failed') 

# Create an instance of each test case. 
testCase1 = TestServer('testFunction1') 
testCase2 = TestServer('testFunction2') 
testCase3 = TestServer('testFunction3') 
testCase4 = TestServer('testFunction4') 

# Add test cases to the test suite. 
testSuite = unittest.TestSuite() 
testSuite.addTest(testCase1) 
testSuite.addTest(testCase2) 
testSuite.addTest(testCase3) 
testSuite.addTest(testCase4) 

# Execute the test suite. 
testRunner = unittest.MyTestRunner(verbosity=2) 
testRunner.run(testSuite) 

我得到的錯誤如下。我還需要一些幫助來定製我的最終測試報告,以便我可以添加一些額外的信息,而不是一個pyunit生成的。我應該在「MyTestResult」類中實現更多內容?

bash-3.2$ python myreport.py 
Traceback (most recent call last): 
    File "myreport.py", line 45, in <module> 
    testRunner = unittest.MyTestRunner(verbosity=2) 
AttributeError: 'module' object has no attribute 'MyTestRunner' 

此外,我找了一些建議,以修改測試報告,該報告如下到來默認。

testRunner = MyTestRunner(verbosity=2) 
# To refer your test runner. 

還有另一個問題:

bash-3.2$ python myreport.py 
testFunction1 (__main__.TestServer) ... ERROR 
testFunction2 (__main__.TestServer) ... ok 
testFunction3 (__main__.TestServer) ... ok 
testFunction4 (__main__.TestServer) ... FAIL 

回答

1

該行應替換。以下是更新MyTestResultMyTestRunner

class MyTestResult(unittest._TextTestResult): 
    def addSuccess(self, test): 
     super(MyTestResult, self).addSuccess(test) 
    def addError(self, test, err): 
     super(MyTestResult, self).addError(test, err) 
    def addFailure(self, test, err): 
     super(MyTestResult, self).addFailure(test, err) 
     # To call parent's method use `super` 

     # OR qualify with parent class 
     # unittest._TextTestResult.addFailure(self, test, err) 


class MyTestRunner(unittest.TextTestRunner): 
    def _makeResult(self): 
     # _makeResult is not called with verbosity, use `self.verbosity` 
     return MyTestResult(self.stream, self.descriptions, self.verbosity) 
+0

使用代碼原樣,我得到以下錯誤:文件 「myreport.py」 49行,在 testRunner.run(測試套件) 文件「/用戶/ anjangam/pyvenv/venv/lib/python2.7/site-packages/unittest.py「,第330行,在__call__中 測試(結果) 文件」/users/anjangam/pyvenv/venv/lib/python2.7/site文件「myreport.py」,第10行,在addError中 super(MyTestResult,self).addError(test());第209行,在__call__中 result.addError(self,self .__ exc_info()) ,錯誤) TypeError:必須是type,而不是classobj – AnilJ

+0

@AnilJ,看看這個:http://ideone.com/AF4n6w – falsetru

+0

使用這行'unittest._TextTestResult.addFailure(self,test,err)'解決了這個問題。 – AnilJ