[Pyunit-interest] testRunner argument of unittest.main()
Brought to you by:
purcell
From: Matthew F. B. <mf...@lo...> - 2003-05-13 02:07:54
|
Here's a question: I'm trying to use unittest.main in combination with a custom TestRunner class (which I'll call myTestRunner). I invoke unittest.main as follows: if __name__ == '__main__': unittest.main(testRunner=myTestRunner()) I would like myTestRunner to be sensitive to command-line arguments such as --verbosity. The problem is that I have to pass an *instance* of myTestRunner to unittest.main, and so it's too early to pass the verbosity value to myTestRunner's constructor since unittest.main hasn't parsed it out yet. Looking through the unittest.py source code that came with Python 2.2.2, I'm wondering if the following change to the unittest.TestProgram class would be an improvement: def __init__(self, module='__main__', defaultTest=None, argv=None, testRunner=TextTestRunner, testLoader=defaultTestLoader): ... def runTests(self): if type(self.testRunner) is types.ClassType: self.testRunner = self.testRunner(verbosity=self.verbosity) result = self.testRunner.run(self.test) sys.exit(not result.wasSuccessful()) Two changes here: 1) The testRunner argument of __init__ defaults to TextTestRunner instead of None.2) The runTests method checks if self.testRunner is a class (TextTestRunner *is* a class), and if so creates an instance of that class, passing it self.verbosity (the part I'm after). Apart from modifying unittest.py itself, is there another way that I could get the behavior I'm after? Thanks, Matthew Barnes ma...@ba... |