Thread: [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... |
From: Jim V. <Jim...@no...> - 2003-05-13 20:07:48
|
Hi Matthew, Since it gives access to the command line parameters, would sys.argv meet your needs? "Matthew F. Barnes" wrote: > 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... > > ------------------------------------------------------- > Enterprise Linux Forum Conference & Expo, June 4-6, 2003, Santa Clara > The only event dedicated to issues related to Linux enterprise solutions > www.enterpriselinuxforum.com > > _______________________________________________ > Pyunit-interest mailing list > Pyu...@li... > https://lists.sourceforge.net/lists/listinfo/pyunit-interest |
From: Matthew F. B. <mf...@lo...> - 2003-05-13 23:16:14
|
Jim Vickroy said: > Since it gives access to the command line parameters, would sys.argv > meet your needs? Not the most "clean" solution, but it would probably work. By that I mean that generally (for me, anyway) the desire is to parse all the command-line arguments *once* in a program. The unittest.main() routine is already doing that work, and the software engineer in me would hate to see that logic replicated elsewhere in the same program. Of course I wasn't asking for the *best* solution, just *a* solution. So you did answer my question. I appreciate the feedback. Matthew Barnes ma...@ba... |
From: robin a. j. <rob...@ea...> - 2003-05-13 23:41:37
|
You might consider using the getopt module to parse the return value from sys.argv. ----- Original Message ----- From: "Matthew F. Barnes" <mf...@lo...> To: <pyu...@li...> Sent: Tuesday, May 13, 2003 05:15 PM Subject: [Pyunit-interest] Re: testRunner argument of unittest.main() > Jim Vickroy said: > > Since it gives access to the command line parameters, would sys.argv > > meet your needs? > > > Not the most "clean" solution, but it would probably work. By that I mean > that generally (for me, anyway) the desire is to parse all the > command-line arguments *once* in a program. The unittest.main() routine > is already doing that work, and the software engineer in me would hate to > see that logic replicated elsewhere in the same program. > > Of course I wasn't asking for the *best* solution, just *a* solution. So > you did answer my question. I appreciate the feedback. > > Matthew Barnes > ma...@ba... > > > > > > ------------------------------------------------------- > Enterprise Linux Forum Conference & Expo, June 4-6, 2003, Santa Clara > The only event dedicated to issues related to Linux enterprise solutions > www.enterpriselinuxforum.com > > _______________________________________________ > Pyunit-interest mailing list > Pyu...@li... > https://lists.sourceforge.net/lists/listinfo/pyunit-interest |
From: Matthew F. B. <mf...@lo...> - 2003-05-14 02:22:44
|
>> Not the most "clean" solution, but it would probably work. By that I >> mean that generally (for me, anyway) the desire is to parse all the >> command-line arguments *once* in a program. The unittest.main() >> routine is already doing that work, and the software engineer in me >> would hate to see that logic replicated elsewhere in the same program. robin and jim said: > You might consider using the getopt module to parse the return value > from sys.argv. True, but my point was that unittest.main() is doing that already, and I wouldn't want to replicate that logic in two or more places in the same program because it then becomes a maintenance issue for me. Ideally I'd like to pass unittest.main() my custom TestRunner class (called myTestRunner) and have unittest.main() create an instance of myTestRunner and supply it the correct verbosity setting with no sys.argv parsing on my part. Currently my solution is to subclass unittest.TestProgram and alter its behavior according to my original suggestion. It seems to work fine. I guess at this point I'm wondering if my original suggestion would be a useful enhancement to the "official" unittest module. Matthew Barnes ma...@ba... |