6

I am using argparse to handle command line arguments. The code was working fine. However, as soon as I am adding unittest.main() in the main, it is not working.

I am getting:

I am here 
option -i not recognized
Usage: testing.py [options] [test] [...]

Options:
  -h, --help       Show this message
  -v, --verbose    Verbose output
  -q, --quiet      Minimal output
  -f, --failfast   Stop on first failure
  -c, --catch      Catch control-C and display results
  -b, --buffer     Buffer stdout and stderr during test runs

Examples:
  testing.py                               - run default set of tests
  testing.py MyTestSuite                   - run suite 'MyTestSuite'
  testing.py MyTestCase.testSomething      - run MyTestCase.testSomething
  testing.py MyTestCase                    - run all 'test*' test methods
                                               in MyTestCase

I am doing like this:

if __name__ == "__main__":
    print "I am here"
    unittest.main()
4
  • There is no option -i so the error would be correct. How do you call the script? Commented Nov 28, 2013 at 12:19
  • I am calling the script like: python testing.py -isVerbose True Commented Nov 28, 2013 at 12:39
  • 1
    Then that is one problem: you should call it as python testing.py --verbose because you have no option declared called isVerbose. Commented Nov 28, 2013 at 12:41
  • No. I have defined isVerbose. args = parser.parse_args() isVerbose=args.isVerbose Commented Nov 28, 2013 at 12:47

2 Answers 2

13

use

runner = unittest.TextTestRunner()
itersuite = unittest.TestLoader().loadTestsFromTestCase(MyTestClass)
runner.run(itersuite)

instead of:

unittest.main()
Sign up to request clarification or add additional context in comments.

2 Comments

Brilliant, it works now. Have not understand why the unittest.main() was failing.
unittest.main() seems to a look at any args passed to the main code, so it was trying to resolve your custom args which, of course, it couldn't.
3

If you don't need any arguments, use

unittest.main(argv=[''])

1 Comment

Excellent addition!

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.