Overload __init__ of unittest.testcase

I want to add two variables to my subclass that inherits from unittest.testcase

like me:

import unittest class mrp_repair_test_case(unittest.TestCase): def __init__(self, a=None, b=None, methodName=['runTest']): unittest.TestCase.__init__(self) self.a= a self.b = b def test1(self): .......... ....... def runtest() mrp_repair_test_case(a=10,b=20) suite = unittest.TestLoader().loadTestsFromTestCase(mrp_repair_test_case) res = unittest.TextTestRunner(stream=out,verbosity=2).run(suite) 

how can i do this: I get this error:

 ValueError: no such test method in ****<class 'mrp_repair.unit_test.test.mrp_repair_test_case'>:**** runTest 

thanks

+4
source share
1 answer

At first glance, it looks like you need to create an instance of mrp_repair_test_case . Current line:

 mrp_repair_test_case(a=10,b=20) 

actually does nothing.

Try (not verified):

 def runtest(): m = mrp_repair_test_case(a=10, b=20) suite = unittest.TestLoader().loadsTestsFromTestCase(m) res = unittest.TextTestRunner(stream=out, verbosity=2).run(suite) 

It is assumed that you have already set 'out' as a stream.

Edit:

By the way, is there a reason why you are not using the setUp method to set these values? That would be normal good practice. Looking at the loadTestsFromTestCase documentation, it looks like it will only accept the class itself, not an instance of it, which would mean that you are more likely working with the unittest module design.

Edit 2:

In response to your additional information, I would set your uid and cursor values ​​separately at the module level before calling the tests. I am not a big fan of global variables, but if I understand correctly, these values ​​will be A) read-only B) always the same for the same client, which avoids most common errors when using them.

Edit 3:

To answer your edit, if you really want to use __init__ , you probably can, but you will have to minimize your own alternative to loadTestsFromTestCase and possibly your own TestSuite (you will need to check the internals for how this works). As I said above, you will work against the existing module design - as much as possible, if you decide to conduct your testing in this way, it will be easier for you to roll your own solution from scratch than to use unittest. Edit : just checked, you will definitely need to roll your own version of TestSuite, since the existing one creates a new instance of TestCaseClass for each test.

+6
source

All Articles