生成自定义pyunit rep

2024-10-02 16:31:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图生成一个自定义的pyunit测试套件执行报告,但是遇到了一个“no attribute”错误。在

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'类中实现什么?在

^{pr2}$

另外,我正在寻找一些修改测试报告的建议,默认如下所示。在

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

Tags: testimportselftruemaindefresunittest