Adding returns to command #729
-
First Check
Commit to Help
Example Codeimport random
import typer
from typer.testing import CliRunner
app = typer.Typer()
@app.command()
def main():
result = random.random()
return result
def test_app() -> None:
runner = CliRunner()
result = runner.invoke(app)
assert result.return_value == 0 DescriptionHi everyone, I am trying to write some end-to-end tests for a To so do, I was hoping to set up a unit test using I was expecting the result of the However, try as I may, the Is there a clean way to achieve what is described in the code snippet above? Cheers Operating SystemLinux Operating System DetailsNo response Typer Version0.9.0 Python Version3.10.13 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Why not |
Beta Was this translation helpful? Give feedback.
-
When working with CLI, the return value is generally discarded. You can pass a return code using |
Beta Was this translation helpful? Give feedback.
When working with CLI, the return value is generally discarded. You can pass a return code using
sys.exit
or Typer's equivalent. That way you can assert on the exit code (result.exit_code
), orprint
(or useTyper.echo
) and assert on the outputresult.output
as suggested by @melsabagh.Outside of the scope of testing, where would you expect the return of the command to go to?