From this great answer I learned to put argument parsing into its own function to simplify unit testing.
From this answer I learned that sometimes you need to throw your own parser errors to get argparse to perform the behaviour you want. E.g.:
if not (args.process or args.upload):
parser.error('No action requested, add -process or -upload')
But it is hard to test if this does what it should since throwing the parser error also exits the program. So something like this TestCase won't work:
def test_no_action_error(self):
'''Test if no action produces correct error'''
with self.assertRaises(ArgumentError) as cm:
args = parse_args(' ')
self.assertEqual('No action requested, add -process or -upload', str(cm.exception))
The comments from the first question suggest this question. But I don't follow how to use this code within a testing file.