Feature: Add support for multiple user inputs as part of typer.testing #932
-
First Check
Commit to Help
Example Codeimport os
import sys
import pytest
from typer.testing import CliRunner
sys.path.append(os.path.join(os.path.dirname(__file__), "../scripts"))
from main import app
runner = CliRunner()
GROUP_NAME2 = "a-test-group"
def test_group_delete1() -> None:
"""Delete a test group which is a member of another group"""
result = runner.invoke(app, ["group", "delete", GROUP_NAME2], input=["y", "y"])
assert result.exit_code == 0
assert "SUCCESS" in result.stdout
assert "Delete group" in result.stdout
assert "Really delete" in result.stdoutDescriptionI have a script which has a function for deleting a group: Rather than producing a different single message that the user is aware the group is a part of another group and to proceed with deletion, the CLI tool prompts the user a second time for input to ensure they really do want to delete the group (and by extension accept the consequents if they opt to move forward). As a result of this, when attempting to write a valid typer test for this, I run into errors. It would be nice if the Thank you in advance for the consideration. Operating SystemmacOS Operating System DetailsSonoma 14.6 Typer Version0.12.3 Python VersionPython 3.11.9 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
You can pass multiple inputs in one string with result = runner.invoke(app, ["group", "delete", GROUP_NAME2], input=("y\n" + "y\n"))Full code: import typer
from typer.testing import CliRunner
app = typer.Typer()
@app.command()
def delete(group: str):
typer.confirm("Are you sure?", abort=True)
if True: # Group has sub-groups
typer.confirm("This group has sub-groups. Are you really sure?", abort=True)
typer.echo("Ok, deleted.")
@app.command()
def add(group: str):
pass
if __name__ == "__main__":
app()
def test_():
runner = CliRunner()
res = runner.invoke(app, ["delete", "GROUP_NAME2"], input=("y\n" + "y\n"))
assert "Ok, deleted." in res.output |
Beta Was this translation helpful? Give feedback.
You can pass multiple inputs in one string with
\nseparator:Full code: