python - 生成自定义 pyunit 报告

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

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

最佳答案

该行应替换为:

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)

关于python - 生成自定义 pyunit 报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40560763/

相关文章:

python-2.7 - 如何为 Portable Python 安装 scikit-learn?

java - 为 TestNG 方法传递对象参数?

python - Python 中的网络摄像头 DirectShow 属性页

python - 我应该如何/在哪里存储未连接到任何 HTTP 请求的 Django 应用程序中的数据?

python - 如何在Python中向argparse添加写入文件选项?

python - Scrapy startproject 给出奇怪的错误

c++ - 当我在测试期间手动设置 errno 时,strerror_r 返回垃圾

unit-testing - serde 实现中的模拟实例

python - django 计算中的 0.01 differecene

python - 根据给出 ValueError 的函数创建一个新列