How to use the typer.testing.CliRunner function in typer

To help you get started, we’ve selected a few typer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tiangolo / typer / tests / test_tutorial / test_commands / test_callback / test_tutorial003.py View on Github external
import subprocess

from typer.testing import CliRunner

from docs_src.commands.callback import tutorial003 as mod

app = mod.app

runner = CliRunner()


def test_app():
    result = runner.invoke(app, ["create", "Camila"])
    assert result.exit_code == 0
    assert "Override callback, running a command" in result.output
    assert "Running a command" not in result.output
    assert "Creating user: Camila" in result.output


def test_for_coverage():
    mod.callback()


def test_script():
    result = subprocess.run(
github tiangolo / typer / tests / test_tutorial / test_options / test_version / test_tutorial003.py View on Github external
import os
import subprocess

import typer
from typer.testing import CliRunner

from docs_src.options.version import tutorial003 as mod

runner = CliRunner()

app = typer.Typer()
app.command()(mod.main)


def test_1():
    result = runner.invoke(app, ["--name", "Rick", "--version"])
    assert result.exit_code == 0
    assert "Awesome CLI Version: 0.1.0" in result.output


def test_2():
    result = runner.invoke(app, ["--name", "rick"])
    assert result.exit_code != 0
    assert "Error: Invalid value for '--name': Only Camila is allowed" in result.output
github tiangolo / typer / tests / test_tutorial / test_commands / test_callback / test_tutorial004.py View on Github external
import subprocess

from typer.testing import CliRunner

from docs_src.commands.callback import tutorial004 as mod

app = mod.app

runner = CliRunner()


def test_help():
    result = runner.invoke(app, ["--help"])
    assert result.exit_code == 0
    assert "Manage users CLI app." in result.output
    assert "Use it with the create command." in result.output
    assert "A new user with the given NAME will be created." in result.output


def test_app():
    result = runner.invoke(app, ["create", "Camila"])
    assert result.exit_code == 0
    assert "Creating user: Camila" in result.output
github tiangolo / typer / tests / test_tutorial / test_subcommands / test_callback_override / test_tutorial004.py View on Github external
import subprocess

from typer.testing import CliRunner

from docs_src.subcommands.callback_override import tutorial004 as mod

runner = CliRunner()

app = mod.app


def test_cli():
    result = runner.invoke(app, ["users", "create", "Camila"])
    assert result.exit_code == 0
    assert "Running a users command" not in result.output
    assert "Callback override, running users command" not in result.output
    assert "I have the high land! Running users command" in result.output
    assert "Creating user: Camila" in result.output


def test_for_coverage():
    mod.default_callback()
    mod.user_callback()
github tiangolo / typer / tests / test_tutorial / test_arguments / test_envvar / test_tutorial003.py View on Github external
import subprocess

import typer
from typer.testing import CliRunner

from docs_src.arguments.envvar import tutorial003 as mod

runner = CliRunner()

app = typer.Typer()
app.command()(mod.main)


def test_help():
    result = runner.invoke(app, ["--help"])
    assert result.exit_code == 0
    assert "[OPTIONS] [NAME]" in result.output
    assert "Arguments:" in result.output
    assert "[env var: AWESOME_NAME;default: World]" not in result.output
    assert "[default: World]" in result.output


def test_call_arg():
    result = runner.invoke(app, ["Wednesday"])
github tiangolo / typer / tests / test_tutorial / test_options / test_callback / test_tutorial003.py View on Github external
import os
import subprocess

import typer
from typer.testing import CliRunner

from docs_src.options.callback import tutorial003 as mod

runner = CliRunner()

app = typer.Typer()
app.command()(mod.main)


def test_1():
    result = runner.invoke(app, ["--name", "Camila"])
    assert result.exit_code == 0
    assert "Validating name" in result.output
    assert "Hello Camila" in result.output


def test_2():
    result = runner.invoke(app, ["--name", "rick"])
    assert result.exit_code != 0
    assert "Error: Invalid value for '--name': Only Camila is allowed" in result.output
github tiangolo / typer / tests / test_tutorial / test_multiple_values / test_multiple_options / test_tutorial001.py View on Github external
import subprocess

import typer
from typer.testing import CliRunner

from docs_src.multiple_values.multiple_options import tutorial001 as mod

runner = CliRunner()
app = typer.Typer()
app.command()(mod.main)


def test_main():
    result = runner.invoke(app)
    assert result.exit_code != 0
    assert "No provided users" in result.output
    assert "Aborted!" in result.output


def test_1_user():
    result = runner.invoke(app, ["--user", "Camila"])
    assert result.exit_code == 0
    assert "Processing user: Camila" in result.output
github tiangolo / typer / tests / test_completion / test_completion_install.py View on Github external
stderr=subprocess.PIPE,
        encoding="utf-8",
        env={
            **os.environ,
            "_TYPER_COMPLETE_TESTING": "True",
            "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True",
        },
    )
    new_text = completion_path.read_text()
    completion_path.unlink()
    assert "complete --command tutorial001.py" in new_text
    assert "completion installed in" in result.stdout
    assert "Completion will take effect once you restart the terminal" in result.stdout


runner = CliRunner()
app = typer.Typer()
app.command()(mod.main)


def test_completion_install_powershell():
    completion_path: Path = Path.home() / f".config/powershell/Microsoft.PowerShell_profile.ps1"
    completion_path_bytes = f"{completion_path}\n".encode("windows-1252")
    text = ""
    if completion_path.is_file():  # pragma: nocover
        text = completion_path.read_text()

    with mock.patch.object(
        shellingham, "detect_shell", return_value=("pwsh", "/usr/bin/pwsh")
    ):
        with mock.patch.object(
            subprocess,
github tiangolo / typer / tests / test_tutorial / test_subcommands / test_tutorial003.py View on Github external
import subprocess

import pytest
from typer.testing import CliRunner

from docs_src.subcommands import tutorial003

runner = CliRunner()


@pytest.fixture()
def mod(monkeypatch):
    with monkeypatch.context() as m:
        m.syspath_prepend(list(tutorial003.__path__)[0])
        from docs_src.subcommands.tutorial003 import main

        return main


@pytest.fixture()
def app(mod):
    return mod.app
github tiangolo / typer / tests / test_tutorial / test_commands / test_options / test_tutorial001.py View on Github external
import subprocess

from typer.testing import CliRunner

from docs_src.commands.options import tutorial001 as mod

app = mod.app

runner = CliRunner()


def test_help():
    result = runner.invoke(app, ["--help"])
    assert result.exit_code == 0
    assert "Commands:" in result.output
    assert "create" in result.output
    assert "delete" in result.output
    assert "delete-all" in result.output
    assert "init" in result.output


def test_create():
    result = runner.invoke(app, ["create", "Camila"])
    assert result.exit_code == 0
    assert "Creating user: Camila" in result.output