Python - unittest尝试调用导入的自定义argparser

时间:2014-01-13 19:01:53

标签: python argparse python-unittest

我有一个单元测试,想要根据unittest的命令行选项调用导入的模块同时执行parse_os,但似乎unittest无法识别该选项,任何想法:

./python testParser.py --mac
option --mac not recognized
Usage: testParser.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:
 testParser.py                               - run default set of tests
 testParser.py MyTestSuite                   - run suite 'MyTestSuite'
 testParser.py MyTestCase.testSomething      - run MyTestCase.testSomething
 testParser.py MyTestCase                    - run all 'test*' test methods
                                           in MyTestCase

我想运行我的unittest程序:python testParser.py --mac

EDITTED:现在通过将'unittest.main()'更改为:

来工作
runner = unittest.TextTestRunner(stream=stderr_file)
itersuite = unittest.TestLoader().loadTestsFromTestCase(TT28046_ForensicSearchSmokeTest)
runner.run(itersuite)

单元测试计划:

import logging
import unittest

from myargparse import *

class MyTest(unittest.TestCase):

   def test_parse_os(self):
        ## Parse the args:
       self.install = install_sw(parse_os(arg=""))
       print 'Which os? %s' % self.install

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    # get the default logger
    logger = logging.getLogger()
    # add a file handler
    logger.addHandler(logging.FileHandler('stdout.txt', mode='w'))
    # set up a stream for all stderr output
    stderr_file = open('stderr.txt', 'w')
    # attach that stream to the testRunner
    unittest.main(testRunner=unittest.TextTestRunner(stream=stderr_file))

我导入的模块:

import argparse
import os
import sys

def parse_os(arg):
    my_os = ''
    parser = argparse.ArgumentParser()
    parser.add_argument("-m", "--mac",
                    action="store_true")
    parser.add_argument("-w", "--win",
                    action="store_true")
    args = parser.parse_args()

    if args.mac:
       print 'Mac'
       my_os = "Mac"
    if args.win:
       print 'Windows'
       my_os = "Windows"
    return my_os

def install_sw(my_os):
    installed_os = None
    if my_os == 'Mac':
        print 'Installing Mac...'
        installed_os = 'Mac'
    if my_os == 'Windows':
        print 'Installing Windows...'
        installed_os = 'Windows'
    return installed_os

5 个答案:

答案 0 :(得分:2)

sys.argv变量是一个简单的list,因此您可以根据自己的意愿修改/替换它。 在这种情况下,我考虑使用上下文管理器:

class SysArgv(object):
    def __init__(self, argv):
        self._old_argv = sys.argv
        sys.argv = argv
    def __enter__(self):
        return self
    def __exit__(self, exc_type, exc_value, tb):
        sys.argv = self._old_argv
        return False

用作:

In [4]: with SysArgv(['a', 'b', 'c']):
   ...:     print(sys.argv)
   ...:     
['a', 'b', 'c']

在您的情况下,简单包装测试代码,如:

with SysArgv(['the_module_name.py', '--mac']):
    # test code goes here

并且argparse模块将看到您想要的参数。

至于在运行测试时将参数传递给unittest模块,可以将argv参数传递给unittest.main。来自文档:

The `argv` argument can be a list of options passed to the program, with the first element being the program name. If not specified or `None`, the values of `sys.argv` are used.

但是在这种情况下,您应该在调用sys.argv之前修改unittest.main变量:

if __name__ == '__main__':
    options = ['name_of_module.py'] + sys.argv[-1:]   # last argument as option for the test
    with SysArgv(sys.argv[:-1]):   # or modify how you want
        unittest.main(argv=options)

答案 1 :(得分:0)

您是否尝试过使用'-m'代替'--mac'?

您也可以尝试:

import optparse

parser = optparse.OptionParser()
parser.add_option("-m", "--mac",
              dest="mac",
              action="store_true",
              help="Run as Mac")
parser.add_option("-w", "--win",
              dest="win",
              action="store_true",
              help="Run as Win")

(options, args) = parser.parse_args()

答案 2 :(得分:0)

感谢大家的建议,但我决定采用此方法来限制对我的计划的更改。

我没有调用'unittest.main()',而是改为调用以下内容:

runner = unittest.TextTestRunner(stream=stderr_file)
itersuite = unittest.TestLoader().loadTestsFromTestCase(MyTest)
runner.run(itersuite)

答案 3 :(得分:0)

基于这里的所有答案,我最初做了这个简单的黑客攻击,它起作用了:

# Change sys.argv before unittest tries to parse our args
sys.argv = [sys.argv[0]]  # Replace with only the first arg
unittest.main()

我意识到我仍然可以使用所有unitest命令行args,在我的情况下,我只是传递了一堆路径,所以任何以“ - ”开头的东西都可以传递,并且不需要破解自unittest.main()argv参数

以来的任何内容
# Pass on options, and more importantly, don't pass on ALL args
options = [sys.argv[0]] + [a for a in sys.argv if a.startswith("-")]
unittest.main(argv=options)

答案 4 :(得分:0)

只过滤掉所有argparse而不是arparse参数,然后将那些不是argparse参数的参数发送到sys.argv,这是一个单元测试用的:

args, notknownargs = parser.parse_known_args()
sys.argv[1:] = notknownargs