argparse和unittest python

时间:2013-11-28 11:54:04

标签: python argparse

我正在使用argparse来处理命令行参数。代码工作正常。但是,只要我在main中添加unittest.main(),它就无法正常工作。

我得到了:

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

我这样做:

if __name__ == "__main__":
    print "I am here"
    unittest.main()

2 个答案:

答案 0 :(得分:10)

使用

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

而不是:

unittest.main()

答案 1 :(得分:0)

如果不需要任何参数,请使用

unittest.main(argv=[''])