I have a fairly complex system that I want to test using python. My test code will interact with the system using a Python module I've already written. There are a few things however, that I haven't been able to figure out, regarding the testing framework. I haven't selected one yet, but obviously I feel directed to unittest.
Passing parameters to the tests. I need to pass a specific ID to many different parts of my test code, depending on which component of the system I am testing. Does unittest provide for this? In other words, right now I just have a test script, which I run like this:
./testscript.py 123 win 32How can I pass the same parameters similarly in a testing framework?unittestprovides forsetUp()andtearDown()methods, but they are called before/after each test method. How can I have functions that are called before/after the entire battery of tests in aTestCase?
Maybe unittest is not what I actually want to use?
-
3for #1 see this stackoverflow.com/questions/1029891/… it can be doneFacundo Casco– Facundo Casco2011年12月14日 16:29:46 +00:00Commented Dec 14, 2011 at 16:29
-
Thanks F.C. that makes logical sense. I'm assuming the only way to actually get parameters to the modules I'm testing with then are globals.Jonathon Reinhart– Jonathon Reinhart2011年12月14日 20:17:54 +00:00Commented Dec 14, 2011 at 20:17
-
So shall I accept my own answer, or would you like to make that an answer so I can accept it?Jonathon Reinhart– Jonathon Reinhart2011年12月15日 00:18:18 +00:00Commented Dec 15, 2011 at 0:18
-
you could add the solution from that answer to yours and accept it in case someone comes here looking for a solutionFacundo Casco– Facundo Casco2011年12月15日 13:36:22 +00:00Commented Dec 15, 2011 at 13:36
2 Answers 2
As for #2, it seems that setUpClass() and tearDownClass() are designed for this. The documentation even shows an example of using it to createExpensiveConnectionObject().
Comments
I use tox to run my tests across multiple configurations. Some of my unittests skip certain tests if an optional module isn't there, which means I needed a check to make sure that the module detection code works. Tox doesn't have an easy way to specify any command-line arguments, so the easiest solution was to go through an environment variable.
F.C.'s pointer to python, unittest: is there a way to pass command line options to the app applies if you decide that extra arguments are the way to go.