2

我在理解如何更改单元测试报告时遇到了一些困难,类似于:

======================================================================    
FAIL: test_equal (__main__.InequalityTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_notequal.py", line 7, in test_equal
    self.assertNotEqual(1, 3-2, "My Custom Message")
AssertionError: 1 == 1

类似于以下的报告:

Line 7: My Custom Message

我如何解析这些报告?

4

2 回答 2

2

经过进一步研究,我的问题可以通过覆盖默认的 TestResult 类来解决,如下所示:Turn some print off in python unittest

或者通过使用一些第三方定制,例如 nose,一个HTMLTestRunner。

于 2013-06-07T13:37:43.630 回答
0

In case you need to create a custom report based on the success/failure of individual test cases, you can create a custom TestRunner, which uses custom TestResult, and in TestResult class, override success and failure methods. This will provide the callback for processing as per requirement.

class CustomTestRunner(TextTestRunner): 
        def _makeResult(self): 
            return CustomTestResult(TestResult)
        def run(self, test) -> unittest.result.TestResult:
            # add implementation as per TextTestRunner run method here

class CustomTestResult:
       def addSuccess(self, test):
           super(CustomTestResult, self).addSuccess(test)
           # your logic to log success cases

       def addFailure(self, test):
           super(CustomTestResult, self).addFailure(test)
           # your logic to log failure cases

For reference, please check python unittest module.

于 2021-02-20T07:03:12.840 回答