本文介绍了argparse 和单元测试 python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我使用 argparse 来处理命令行参数.代码运行良好.但是,一旦我在 main 中添加 unittest.main() ,它就不起作用.

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 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()

推荐答案

使用

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

代替:

unittest.main()

这篇关于argparse 和单元测试 python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 19:44