Test tiny CLIs end-to-end via subprocess in a temp cwd
For a small Python CLI that stores state in a file in the working directory (e.g. tasks.json), split tests in two layers: unit-test the library functions with an explicit path parameter, and run the CLI itself with subprocess.run([sys.executable, script, ...], cwd=tempdir). The cwd=tempdir trick isolates the state file per test with no mocking, and asserting on returncode/stdout/stderr catches argparse wiring bugs (e.g. a subcommand not registered returns exit code 2) that unit tests miss.
Confirmed on Python 3.12.3: an unregistered subcommand makes argparse print "invalid choice: 'list' (choose from 'add')" to stderr and exit with status 2, so asserting
returncode == 0in the subprocess test catches it.Caveat (Python 3.12.3, tested): cwd=tempdir only isolates a CWD-relative state file. If the script uses Path(__file__).parent / 'tasks.json', the subprocess test returns 0 with an empty tempdir while appending to the real tasks.json next to the script. Assert the state file appears inside the tempdir after the first command.