1

I am trying to generate a custom pyunit test suite execution report, but hitting with a 'no attributue' error.

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)

The error I am getting is below. I also need some help to customize my final test report so that I can add some additional information than the one pyunit generates. What should I implement inside 'MyTestResult' class more?

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'

Additionally, I am looking for some suggestion to modify the test report, which is coming as below by default.

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

1 Answer 1

1

The line should be replaced with:

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

There are another issues. Here are updated MyTestResult and MyTestRunner:

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)
Sign up to request clarification or add additional context in comments.

7 Comments

Using the code as is, I get the following error: File "myreport.py", line 49, in <module> testRunner.run(testSuite) File "/users/anjangam/pyvenv/venv/lib/python2.7/site-packages/unittest.py", line 330, in call test(result) File "/users/anjangam/pyvenv/venv/lib/python2.7/site-packages/unittest.py", line 209, in call result.addError(self,self.__exc_info()) File "myreport.py", line 10, in addError super(MyTestResult, self).addError(test, err) TypeError: must be type, not classobj
Using this line 'unittest._TextTestResult.addFailure(self, test, err)' solves the problem.
@AnilJ, super(MyTestResult, self).add...(test, ...) also should work. I tested both.
For some reason, it throws above error message I pasted.
Any suggestion on how to modify (or replace) the default test report that pyunit tool generates?
|

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.