November 30, 2021

Testing Argparse Applications - the Better Way

When you are creating command line applications in Python, you probably heard of argparse, which is a great library for exactly doing this, and it is even included in Python’s standard library. Imagine you have created the following argparse application: <main.py> import argparse def main(): parser = argparse.ArgumentParser() parser.add_argument('--name', required=True) args = parser.parse_args() print(f'Hello {args.name}') if __name__ == '__main__': sys.exit(main()) Looks straightforward, works great, but at one point, you certainly want to add tests for it, right?...