Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

argparse and unittest python

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()
like image 270
Exploring Avatar asked Nov 28 '13 11:11

Exploring


1 Answers

use

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

instead of:

unittest.main()
like image 174
Matt Clarke Avatar answered Oct 16 '22 09:10

Matt Clarke