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()
Hirusha Fernando
  • 1,156
  • 10
  • 29
Exploring
  • 2,493
  • 11
  • 56
  • 97

2 Answers2

12

use

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

instead of:

unittest.main()
Matt Clarke
  • 757
  • 4
  • 15
  • Brilliant, it works now. Have not understand why the unittest.main() was failing. – Exploring Nov 28 '13 at 12:52
  • 2
    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. – Matt Clarke Nov 28 '13 at 13:33
2

If you don't need any arguments, use

unittest.main(argv=[''])
Ethan Zhao
  • 21
  • 1