0

I have a Python module that uses the argparse library. How do I write tests for that section of the code base?

show_version.py

import argparse
def main():
    parser = argparse.ArgumentParser(add_help=False)
    parser.add_argument('-j', required=True)
    parser.add_argument('-e', required=True)
    parser.add_argument('-h', required=True)
    parser.add_argument('-g')
    args = parser.parse_args()

    try:
       ...
    except Exception as e:
        ...
    ...

if __name__ == '__main__':
    main()
  • Because it normally parses `sys.argv`, and sends error messages to `sys.stdout` or `stderr`, and exits, it's a bit tricky. Plus the `unittest` drivers parses `sys.argv`. Dev. versions of Python include a `test_argparse.py` unittesting file that may give you ideas. But it is large and complicated. – hpaulj Jun 14 '18 at 04:48
  • From the `related` sidebar: https://stackoverflow.com/questions/18160078/how-do-you-write-tests-for-the-argparse-portion-of-a-python-module, and https://stackoverflow.com/questions/37697502/python-unittest-for-argparse, and https://stackoverflow.com/questions/42331049/how-to-test-python-classes-that-depend-on-argparse – hpaulj Jun 14 '18 at 04:49

0 Answers0