2

I'm having some difficulty understanding how I would go about changing a unittest report similar to:

======================================================================    
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

to a report resembling:

Line 7: My Custom Message

How could I parse these reports?

2 Answers 2

2

After further research, my problem can be solved by overriding the default TestResult class as seen here: Turn some print off in python unittest

or by using some third-party customization such as nose, an HTMLTestRunner.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.