Testing Python command line apps
Recently I wrote a Python command line app and had some trouble finding a good example of how to test it using best practices. Some ideas I ran across in my search:
- Use a 3rd party CLI-testing solution like ScriptTest or a framework that includes testing like pyCLI.
I write my other tests with unittest
and run them with nose. I don’t think there’s anything special about a CLI that requires the rest of my team to learn YATT (yet another testing tool).
- Use
sys.argv
,subprocess
or some combination of both.
This feels very hacky to me. Writing a Python CLI using sys.argv is a little silly considering the more robust options out there. argparse
is in the standard library. 3rd party tools like clint and cliff are frameworks that seem promising. A testing solution that plays to the strength of these tools is ideal.
Personally, I use argparse
. It does everything I need and its API is...