-
I realize the right fix might be to restructure the program, but is there any way to test a function with the click decorators on it without Simple self-contained example: #!/usr/bin/env python3
import os
import sys
import click
import pytest
@click.command()
@click.option("--some-option", is_flag=True)
@click.option("--other-option", is_flag=True)
@click.option("--dry-run", is_flag=True)
def main(some_option: bool, other_option: bool, dry_run: bool):
foobar = os.environ.get("FOO_BAR")
if not foobar:
sys.stderr.write("FOO_BAR not set\n")
sys.exit(1)
# Normally not in the same place / file, but keeping it self-contained here
def test_main(capsys):
with pytest.raises(SystemExit) as e_info:
main()
_, err = capsys.readouterr()
assert err.startswith("FOO_BAR not set")
assert e_info.value.code == 1
if __name__ == "__main__":
main() If this is run in a directory and the file is named
|
Beta Was this translation helpful? Give feedback.
Answered by
davidism
Sep 1, 2023
Replies: 1 comment 4 replies
-
Yes, see the documentation on testing: https://click.palletsprojects.com/en/8.1.x/testing/ |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
wyardley
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, see the documentation on testing: https://click.palletsprojects.com/en/8.1.x/testing/