Problem with sys.argv[1] when unittest module is in a script

chrisg picture chrisg · May 11, 2010 · Viewed 11k times · Source

I have a script that does various things and access paramenters using sys.argv but when the script gets to the unittest part of the code it says there is no module for this. The script that I have is:

class MyScript():

    def __init__(self):
        self.value = sys.argv[1]

    def hello(self):
        print self.value

    def suite(self):
        modules_to_test = ('external_sanity_onvif', 'starttest')
        alltests = unittest.TestSuite()
        for module in map(__import__, modules_to_test):
            alltests.addTest(unittest.findTestCases(module))
        return alltests


if __name__ == '__main__': 
    Run = MyScript()
    Run.hello()
    log_file = 'log_file.txt'
    test_file = open(log_file, "w") 
    runner = unittest.TextTestRunner(test_file)
    unittest.main(defaultTest='Run.suite', testRunner=runner)

Say I enter ./script.py Hello in the command line. The error I get is:

AttributeError: 'module' object has no attribute 'Hello'

If I remove the unittest module it works. Also if I remove the testrunner log and leave it at:

unittest.main(defaultTest='Run.suite')

This still doesn't work.

Can anyone help.

Thanks

I tried this hack but it still tries to read the sys.argv.

project = sys.argv[4:]
sys.argv = sys.argv[0:4]

I have tried this with just argv but it still tires to read the extra parameters.

Answer

Owen S. picture Owen S. · May 15, 2010

The problem is that unittest.main() wants your precious argv for its own use! It uses either the argv you give it as a function parameter, or sys.argv if you don't give it argv explicitly, and tries to load tests named the arguments you give. In this case, this means it's looking for either a submodule called Hello, a TestCase class named Hello, a test case method within a test case class named Hello, or a callable object called Hello that returns a TestCase or TestSuite instance, all within your module 'script'.

There are several ways to fix this:

  • Bypass unittest.main() and call lower-level unittest functions yourself to set up and run the test cases you have in mind.
  • Remove your code's dependency on sys.argv, and use the unittest.main() behavior to your advantage. If your module isn't meant to be run independently except as a unit test, this probably makes sense, since callers of your module may not be expecting you to read from their argv!
  • Separate the test code and main routine into a separate test module. You'd still have to figure out how to get the right argv into your code though from the test module.
  • Specify argv=[sys.argv[0]] as an argument to unittest.main(); that should keep it from trying to read yours.