Capture output with pytest #714
-
First Check
Commit to Help
Example Codeimport typer
from rich import print
from typing_extensions import Annotated
app = typer.Typer(add_completion=False, no_args_is_help=True, rich_markup_mode="rich")
@app.command()
def hello(name: str):
print(f"Hello {name}")
if __name__ == "__main__":
typer.run(hello) DescriptionHow do I capture the output of the CLI execution when executing with pytest? For example, the following unit test fails:
I would have expected some sort of captured output like
This issue seems to suggest it should just work? Operating SystemmacOS Operating System DetailsNo response Typer Version0.9.0 Python Version3.11.6 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The runner does its own output capturing: https://typer.tiangolo.com/tutorial/testing/#check-the-result - unfortunately there seems to be no way to turn it off, so you'd need to Also see (the |
Beta Was this translation helpful? Give feedback.
The runner does its own output capturing: https://typer.tiangolo.com/tutorial/testing/#check-the-result - unfortunately there seems to be no way to turn it off, so you'd need to
print(result.stdout)
at some point (possibly via a helper function or fixture).Also see (the
CliRunner
is just a thin wrapper over Click):