1

I am using windows with python2.7. My program is quite simple. Taking an input via command line and checking if it is '5'.

Code:

import unittest
import sys

a=sys.argv[1]

print a

class TestCase(unittest.TestCase):
    """My tests"""

    def test_is_number_is_five(self):
        self.assertEqual('5',a)

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

And running it using:

>>python test.py 5

I am getting this error:

Traceback (most recent call last):
  File "C:\Python27\lib\unittest\main.py", line 94, in __init__
    self.parseArgs(argv)
  File "C:\Python27\lib\unittest\main.py", line 149, in parseArgs
    self.createTests()
  File "C:\Python27\lib\unittest\main.py", line 158, in createTests
    self.module)
  File "C:\Python27\lib\unittest\loader.py", line 130, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "C:\Python27\lib\unittest\loader.py", line 100, in loadTestsFromName
    parent, obj = obj, getattr(obj, part)
AttributeError: 'module' object has no attribute '5'
0

1 Answer 1

3

unittest.main() will obtain a list of tests to run from the command line. Hence it is trying to find a test named 5 in your module, but it fails because there is no such test.

To work around the problem you can pass your own argv values to unittest.main():

if __name__ == '__main__':
    unittest.main(argv=sys.argv[:1])

This passes only the first argument, which is the script file name. It does, however, break some the functionality of unittest.main() should you want to pass test names on the command line.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.