Python, unit test - Pass command line arguments to setUp of unittest.TestCase

Posted by sberry2A on Stack Overflow See other posts from Stack Overflow or by sberry2A
Published on 2009-12-03T19:05:40Z Indexed on 2010/04/15 16:33 UTC
Read the original article Hit count: 1080

Filed under:
|

I have a script that acts as a wrapper for some unit tests written using the Python unittest module. In addition to cleaning up some files, creating an output stream and generating some code, it loads test cases into a suite using

unittest.TestLoader().loadTestsFromTestCase()

I am already using optparse to pull out several command-line arguments used for determining the output location, whether to regenerate code and whether to do some clean up. I also want to pass a configuration variable, namely an endpoint URI, for use within the test cases.

I realize I can add an OptionParser to the setUp method of the TestCase, but I want to instead pass the option to setUp. Is this possible using loadTestsFromTestCase()? I can iterate over the returned TestSuite's TestCases, but can I manually call setUp on the TestCases?

** EDIT ** I wanted to point out that I am able to pass the arguments to setUp if I iterate over the tests and call setUp manually like:

(options, args) = op.parse_args()
suite = unittest.TestLoader().loadTestsFromTestCase(MyTests.TestSOAPFunctions)
for test in suite:
    test.setUp(options.soap_uri)

However, I am using xmlrunner for this and its run method takes a TestSuite as an argument. I assume it will run the setUp method itself, so I would need the parameters available within the XMLTestRunner.

I hope this makes sense.

© Stack Overflow or respective owner

Related posts about python

Related posts about unit-testing