Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest Anthony's solution for testing argparse #43

Open
1 of 3 tasks
bittner opened this issue May 29, 2023 · 1 comment
Open
1 of 3 tasks

Suggest Anthony's solution for testing argparse #43

bittner opened this issue May 29, 2023 · 1 comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request good first issue Good for newcomers

Comments

@bittner
Copy link
Member

bittner commented May 29, 2023

At PyCon 23 I was suggested 📺 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.

  1. Pass an argv variable to the argument parser's parse_args() function.
  2. 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 argparse example project template could be adjusted to incorporate this idea.
  • Optionally, investigate if a similar approach is also possible with Docopt.
@bittner bittner added documentation Improvements or additions to documentation enhancement New feature or request good first issue Good for newcomers labels May 29, 2023
@bittner
Copy link
Member Author

bittner commented May 29, 2023

For Docopt the identical approach seems to be possible. The docopt() function takes argv as a second argument and reads sys.argv when it is None. Unsurprisingly, this is also supported by Docopt-ng.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant