Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions typer/testing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
"""Testing utilities for Typer applications.

Provides a `CliRunner` that wraps Click's test runner with Typer-aware
`invoke`, so you can test `Typer` apps directly without converting them
to Click commands by hand.

Read more in the
[Typer docs for Testing](https://typer.tiangolo.com/tutorial/testing/).
"""

from collections.abc import Mapping, Sequence
from typing import IO, Any

Expand All @@ -8,6 +18,36 @@


class CliRunner(ClickCliRunner):
"""A test runner for `Typer` applications.

Extends Click's `CliRunner` with a Typer-aware `invoke` method that
accepts a `Typer` instance directly, removing the need to call
`typer.main.get_command` in every test.

Read more in the
[Typer docs for Testing](https://typer.tiangolo.com/tutorial/testing/).

## Example

```python
from typer.testing import CliRunner
import typer

app = typer.Typer()

@app.command()
def hello(name: str) -> None:
typer.echo(f"Hello {name}")

runner = CliRunner()

def test_hello() -> None:
result = runner.invoke(app, ["World"])
assert result.exit_code == 0
assert "Hello World" in result.output
```
"""

def invoke( # type: ignore
self,
app: Typer,
Expand All @@ -18,6 +58,14 @@ def invoke( # type: ignore
color: bool = False,
**extra: Any,
) -> Result:
"""Invoke a `Typer` app in an isolated environment for testing.

Converts the `Typer` instance to a Click command and delegates to
Click's `CliRunner.invoke`.

Read more in the
[Typer docs for Testing](https://typer.tiangolo.com/tutorial/testing/).
"""
use_cli = _get_command(app)
return super().invoke(
use_cli,
Expand Down
Loading