python, unittest: is there a way to pass command line options to the app

后端 未结 5 966
余生分开走
余生分开走 2020-12-04 14:22

I have a module that imports unittest and has some TestCases. I would like to accept some command line options (for example below, the name of a data file), but when I try t

相关标签:
5条回答
  • 2020-12-04 14:43

    Building on Alex's answer, it's actually pretty easy to do using argparse:

    if __name__ == '__main__':
        parser = argparse.ArgumentParser()
        parser.add_argument('--input', default='My Input')
        parser.add_argument('filename', default='some_file.txt')
        parser.add_argument('unittest_args', nargs='*')
    
        args = parser.parse_args()
        # TODO: Go do something with args.input and args.filename
    
        # Now set the sys.argv to the unittest_args (leaving sys.argv[0] alone)
        sys.argv[1:] = args.unittest_args
        unittest.main()
    

    I haven't tested all of the flags you can pass into unittest to see if they work or not, but passing test names in does work, e.g.:

    python test.py --input=foo data.txt MyTest
    

    Runs MyTest with foo and data.txt.

    0 讨论(0)
  • 2020-12-04 14:48

    You should not take arguments and options to run unittests, as you make them run under different, less predictable conditions this way. You should figure out why you need to run tests with different data, and make you test suite complete enough to cover the ground of all data sets without being run differently each time.

    0 讨论(0)
  • 2020-12-04 14:53

    For small standalone apps, I use an initial sentinel option (-t) and call unittest.main() before calling argparse.ArgumentParser()

    if __name__ == "__main__":
        if len(sys.argv) > 1 and sys.argv[1] in ["-t", "--test"]:
            del(sys.argv[1])
            sys.exit(unittest.main()) # pass sys.argv[
    
        p = argparse.ArgumentParser()
        . . .
    
    0 讨论(0)
  • 2020-12-04 15:04

    In your if __name__ == '__main__': section, which you're not showing us, you'll need to optparse and then del sys.argv[1:] before you pass control to unittest code, so that the latter code doesn't try to interpret your command line options again when you've already dealt with them. (It's a bit harder to have some options of your own and also pass some down to unittest, though it can be done if you do have such complex needs).

    0 讨论(0)
  • 2020-12-04 15:04

    I thought I'd share my solution for adding a --debug switch to the test to control the logger:

    if __name__=='__main__':
         parser = argparse.ArgumentParser(description="Build a compilation script")
         parser.add_argument('--debug', help='Turn on debug messages', action='store_true', default=False)
    
         args = parser.parse_args()
    
         if args.debug:
             log_level = logging.DEBUG
         else:
             log_level = logging.INFO
    
         logging.basicConfig(level=log_level)
         sys.argv.pop()
         unittest.main()
    

    Then I extended unittest.TestCase to add logging:

     class mcs_TestCase(unittest.TestCase, object):
         def __init__(self, *args, **kwargs):
             super(mcs_TestCase,self).__init__(*args,**kwargs)
             logging.basicConfig()
             self.logger = logging.getLogger(__name__)
             ...
    

    Now I can turn messages on and off in my test using --debug, but it gets ignored in regular regressions.

    0 讨论(0)
提交回复
热议问题