【问题标题】:Generating custom pyunit report生成自定义 pyunit 报告
【发布时间】:2016-11-12 07:49:59
【问题描述】:

我正在尝试生成自定义 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'

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

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

【问题讨论】:

    标签: python python-2.7 unit-testing python-3.x python-unittest


    【解决方案1】:

    该行应替换为:

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

    还有一个问题。这里更新了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)
    

    【讨论】:

    • 按原样使用代码,我收到以下错误:文件“myreport.py”,第 49 行,在 testRunner.run(testSuite) 文件“/users/anjangam/pyvenv/venv /lib/python2.7/site-packages/unittest.py”,第 330 行,在 call 测试(结果)文件“/users/anjangam/pyvenv/venv/lib/python2.7/site -packages/unittest.py”,第 209 行,在 call result.addError(self,self.__exc_info()) 文件“myreport.py”,第 10 行,在 addError super(MyTestResult, self) .addError(test, err) TypeError: 必须是类型,不是classobj
    • 使用这一行 'unittest._TextTestResult.addFailure(self, test, err)' 解决了问题。
    • @AnilJ, super(MyTestResult, self).add...(test, ...) 也应该工作。我都测试了。
    • 由于某种原因,它抛出了我粘贴的错误消息。
    • 关于如何修改(或替换)pyunit工具生成的默认测试报告有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 2023-03-09
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多