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

孤人 提交于 2019-11-29 01:53:40

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.

If you don't need the command-line features of the unittest module, you can make the optparse and unittest modules play well together by modifying sys.argv just before calling unittest.main()

Try this just before your unittest.main() call:

del sys.argv[1:]

This removes your command line arguments before unittest sees them.

If you aren't using the optparse module, you can do this instead:

my_args = sys.argv[1:]
del sys.argv[1:]
# do_stuff(my_args)
unittest.main()

Generally speaking, the idea behind unit testing is that the tests run without an external driver. E.g. you don't want to depend on some input from the command line. The solution that you're looking for may be through the use of Fixtures?

import sys
import unittest

class MyScript(unittest.TestCase):
    def setUp(self):
        self.value = "Default Value"

    def setHello(self, value):
        self.value = value

    def hello(self):
        return self.value

class UserA(MyScript):
    def setUp(self):
        self.setHello("UserA")

    def testMyName(self):
        self.failUnlessEqual(self.hello(), "UserA")

class UserB(MyScript):
    def setUp(self):
        self.setHello("UserB")

    def testMyName(self):
        self.failUnlessEqual(self.hello(), "UserB")

if __name__ == '__main__': 
    unittest.main()

Python gives you all of the information it can, and unless you want to post the whole set of things you're trying to test we can only guess at what you're trying to accomplish. The error says there's no attribute named "Hello", so maybe the modules you're trying to test should get such a variable, function, or otherwise.

Is there any reason why you're not just using unittest.main()?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!