Add tests for examples in CLI Options

This commit is contained in:
Sebastián Ramírez 2019-12-29 10:24:31 +01:00
parent 345446321e
commit 8596200b7c
7 changed files with 254 additions and 0 deletions

View file

@ -0,0 +1,48 @@
import subprocess
import typer
from typer.testing import CliRunner
from options import tutorial001 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 "Say hi to NAME, optionally with a --lastname." in result.output
assert "If --formal is used, say hi very formally." in result.output
assert "Last name of person to greet." in result.output
assert "Say hi formally." in result.output
def test_1():
result = runner.invoke(app, ["Camila"])
assert result.exit_code == 0
assert "Hello Camila" in result.output
def test_option_lastname():
result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez"])
assert result.exit_code == 0
assert "Hello Camila Gutiérrez" in result.output
def test_formal():
result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez", "--formal"])
assert result.exit_code == 0
assert "Good day Ms. Camila Gutiérrez." in result.output
def test_script():
result = subprocess.run(
["coverage", "run", mod.__file__, "--help"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
assert "Usage" in result.stdout

View file

@ -0,0 +1,40 @@
import subprocess
import typer
from typer.testing import CliRunner
from options import tutorial002 as mod
runner = CliRunner()
app = typer.Typer()
app.command()(mod.main)
def test_1():
result = runner.invoke(app, ["Camila"])
assert result.exit_code != 0
assert 'Error: Missing option "--lastname".' in result.output
def test_option_lastname():
result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez"])
assert result.exit_code == 0
assert "Hello Camila Gutiérrez" in result.output
def test_help():
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "--lastname TEXT" in result.output
assert "[required]" in result.output
def test_script():
result = subprocess.run(
["coverage", "run", mod.__file__, "--help"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
assert "Usage" in result.stdout

View file

@ -0,0 +1,41 @@
import subprocess
import typer
from typer.testing import CliRunner
from options import tutorial003 as mod
runner = CliRunner()
app = typer.Typer()
app.command()(mod.main)
def test_option_lastname():
result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez"])
assert result.exit_code == 0
assert "Hello Camila Gutiérrez" in result.output
def test_option_lastname_prompt():
result = runner.invoke(app, ["Camila"], input="Gutiérrez")
assert result.exit_code == 0
assert "Lastname: " in result.output
assert "Hello Camila Gutiérrez" in result.output
def test_help():
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "--lastname TEXT" in result.output
assert "[required]" in result.output
def test_script():
result = subprocess.run(
["coverage", "run", mod.__file__, "--help"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
assert "Usage" in result.stdout

View file

@ -0,0 +1,41 @@
import subprocess
import typer
from typer.testing import CliRunner
from options import tutorial004 as mod
runner = CliRunner()
app = typer.Typer()
app.command()(mod.main)
def test_option_lastname():
result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez"])
assert result.exit_code == 0
assert "Hello Camila Gutiérrez" in result.output
def test_option_lastname_prompt():
result = runner.invoke(app, ["Camila"], input="Gutiérrez")
assert result.exit_code == 0
assert "Please tell me your last name: " in result.output
assert "Hello Camila Gutiérrez" in result.output
def test_help():
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "--lastname TEXT" in result.output
assert "[required]" in result.output
def test_script():
result = subprocess.run(
["coverage", "run", mod.__file__, "--help"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
assert "Usage" in result.stdout

View file

@ -0,0 +1,50 @@
import subprocess
import typer
from typer.testing import CliRunner
from options import tutorial005 as mod
runner = CliRunner()
app = typer.Typer()
app.command()(mod.main)
def test_prompt():
result = runner.invoke(app, input="Old Project\nOld Project\n")
assert result.exit_code == 0
assert "Deleting project Old Project" in result.output
def test_prompt_not_equal():
result = runner.invoke(
app, input="Old Project\nNew Spice\nOld Project\nOld Project\n"
)
assert result.exit_code == 0
assert "Error: the two entered values do not match" in result.output
assert "Deleting project Old Project" in result.output
def test_option():
result = runner.invoke(app, ["--project-name", "Old Project"])
assert result.exit_code == 0
assert "Deleting project Old Project" in result.output
assert "Project name: " not in result.output
def test_help():
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "--project-name TEXT" in result.output
assert "[required]" in result.output
def test_script():
result = subprocess.run(
["coverage", "run", mod.__file__, "--help"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
assert "Usage" in result.stdout

View file

@ -0,0 +1,34 @@
import subprocess
import typer
from typer.testing import CliRunner
from options import tutorial006 as mod
runner = CliRunner()
app = typer.Typer()
app.command()(mod.main)
def test_call():
result = runner.invoke(app)
assert result.exit_code == 0
assert "Hello Wade Wilson" in result.output
def test_help():
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "--fullname TEXT" in result.output
assert "[default: Wade Wilson]" in result.output
def test_script():
result = subprocess.run(
["coverage", "run", mod.__file__, "--help"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
assert "Usage" in result.stdout