February 2022
Intermediate to advanced
274 pages
6h 28m
English
A great feature of Typer is that it provides a testing interface. With it, we can call our application without having to resort to using subprocess.run, which is good, because we can’t mock stuff running in a separate process. (We looked at a short example of using subprocess.run with test_version_v1 in Using capsys.) We just need to give the runner’s invoke function our app—cards.app—and a list of strings that represents the command.
Here’s an example of invoking the version function:
| | from typer.testing import CliRunner |
| | from cards.cli import app |
| | |
| | runner = CliRunner() |
| | |
| | |
| | def test_typer_runner(): |
| | result = runner.invoke(app, ["version"]) |
| | print() |
| | print(f"version: ... |
Read now
Unlock full access