At PyCon 23 I was suggested :tv: a video by @asottile who shows how to easily get back the control that argparse takes away from you. Two tricks, which ultimately make it unnecessary to mock sys.argv and use the shell function this project provides.
Pass an argv variable to the argument parser's parse_args() function.
Use Pytest's capsys fixture to evaluate stdout and stderr.
Suggested Changes
[ ] It might be helpful to mention this alternative approach in our documentation.
[ ] Alternatively, or in addition, the argparseexample project template could be adjusted to incorporate this idea.
[x] Optionally, investigate if a similar approach is also possible with Docopt.
At PyCon 23 I was suggested :tv: a video by @asottile who shows how to easily get back the control that
argparse
takes away from you. Two tricks, which ultimately make it unnecessary to mocksys.argv
and use theshell
function this project provides.argv
variable to the argument parser'sparse_args()
function.capsys
fixture to evaluatestdout
andstderr
.Suggested Changes
argparse
example project template could be adjusted to incorporate this idea.