Skip to content

Commit c92fddc

Browse files
authored
✅ Fix testing, add tests
Fix testing tools, add first tests
2 parents 39e034e + 45f995c commit c92fddc

File tree

7 files changed

+179
-3
lines changed

7 files changed

+179
-3
lines changed

scripts/test.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ set -x
66
export PYTHONPATH=./docs/src
77
pytest --cov=typer --cov=tests --cov=docs/src --cov-report=term-missing ${@}
88
bash ./scripts/lint.sh
9+
# Check README.md is up to date
10+
diff --brief docs/index.md README.md
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1+
import subprocess
2+
13
import typer
24
from typer.testing import CliRunner
35

4-
from first_steps.tutorial001 import main
6+
from first_steps import tutorial001 as mod
57

68
runner = CliRunner()
79

810

911
def test_cli():
1012
app = typer.Typer()
11-
app.command()(main)
13+
app.command()(mod.main)
1214
result = runner.invoke(app, [])
1315
assert result.output == "Hello World\n"
16+
17+
18+
def test_script():
19+
result = subprocess.run(
20+
["coverage", "run", "--parallel-mode", mod.__file__, "--help"],
21+
stdout=subprocess.PIPE,
22+
stderr=subprocess.PIPE,
23+
encoding="utf-8",
24+
)
25+
assert "Usage" in result.stdout
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import subprocess
2+
3+
import typer
4+
from typer.testing import CliRunner
5+
6+
from first_steps import tutorial002 as mod
7+
8+
runner = CliRunner()
9+
10+
app = typer.Typer()
11+
app.command()(mod.main)
12+
13+
14+
def test_1():
15+
result = runner.invoke(app, [])
16+
assert result.exit_code != 0
17+
assert 'Error: Missing argument "NAME"' in result.output
18+
19+
20+
def test_2():
21+
result = runner.invoke(app, ["Camila"])
22+
assert result.exit_code == 0
23+
assert "Hello Camila" in result.output
24+
25+
26+
def test_script():
27+
result = subprocess.run(
28+
["coverage", "run", "--parallel-mode", mod.__file__, "--help"],
29+
stdout=subprocess.PIPE,
30+
stderr=subprocess.PIPE,
31+
encoding="utf-8",
32+
)
33+
assert "Usage" in result.stdout
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import subprocess
2+
3+
import typer
4+
from typer.testing import CliRunner
5+
6+
from first_steps import tutorial003 as mod
7+
8+
runner = CliRunner()
9+
10+
app = typer.Typer()
11+
app.command()(mod.main)
12+
13+
14+
def test_1():
15+
result = runner.invoke(app, ["Camila"])
16+
assert result.exit_code != 0
17+
assert 'Error: Missing argument "LASTNAME"' in result.output
18+
19+
20+
def test_2():
21+
result = runner.invoke(app, ["Camila", "Gutiérrez"])
22+
assert result.exit_code == 0
23+
assert "Hello Camila Gutiérrez" in result.output
24+
25+
26+
def test_script():
27+
result = subprocess.run(
28+
["coverage", "run", "--parallel-mode", mod.__file__, "--help"],
29+
stdout=subprocess.PIPE,
30+
stderr=subprocess.PIPE,
31+
encoding="utf-8",
32+
)
33+
assert "Usage" in result.stdout
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import subprocess
2+
3+
import typer
4+
from typer.testing import CliRunner
5+
6+
from first_steps import tutorial004 as mod
7+
8+
runner = CliRunner()
9+
10+
app = typer.Typer()
11+
app.command()(mod.main)
12+
13+
14+
def test_1():
15+
result = runner.invoke(app, ["Camila", "Gutiérrez"])
16+
assert result.exit_code == 0
17+
assert "Hello Camila Gutiérrez" in result.output
18+
19+
20+
def test_formal_1():
21+
result = runner.invoke(app, ["Camila", "Gutiérrez", "--formal"])
22+
assert result.exit_code == 0
23+
assert "Good day Ms. Camila Gutiérrez." in result.output
24+
25+
26+
def test_formal_2():
27+
result = runner.invoke(app, ["Camila", "--formal", "Gutiérrez"])
28+
assert result.exit_code == 0
29+
assert "Good day Ms. Camila Gutiérrez." in result.output
30+
31+
32+
def test_formal_3():
33+
result = runner.invoke(app, ["--formal", "Camila", "Gutiérrez"])
34+
assert result.exit_code == 0
35+
assert "Good day Ms. Camila Gutiérrez." in result.output
36+
37+
38+
def test_script():
39+
result = subprocess.run(
40+
["coverage", "run", "--parallel-mode", mod.__file__, "--help"],
41+
stdout=subprocess.PIPE,
42+
stderr=subprocess.PIPE,
43+
encoding="utf-8",
44+
)
45+
assert "Usage" in result.stdout
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import subprocess
2+
3+
import typer
4+
from typer.testing import CliRunner
5+
6+
from first_steps import tutorial005 as mod
7+
8+
runner = CliRunner()
9+
10+
app = typer.Typer()
11+
app.command()(mod.main)
12+
13+
14+
def test_help():
15+
result = runner.invoke(app, ["--help"])
16+
assert result.exit_code == 0
17+
assert "--lastname TEXT" in result.output
18+
assert "--formal / --no-formal" in result.output
19+
20+
21+
def test_1():
22+
result = runner.invoke(app, ["Camila"])
23+
assert result.exit_code == 0
24+
assert "Hello Camila" in result.output
25+
26+
27+
def test_option_lastname():
28+
result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez"])
29+
assert result.exit_code == 0
30+
assert "Hello Camila Gutiérrez" in result.output
31+
32+
33+
def test_option_lastname_2():
34+
result = runner.invoke(app, ["--lastname", "Gutiérrez", "Camila"])
35+
assert result.exit_code == 0
36+
assert "Hello Camila Gutiérrez" in result.output
37+
38+
39+
def test_formal_1():
40+
result = runner.invoke(app, ["Camila", "--lastname", "Gutiérrez", "--formal"])
41+
assert result.exit_code == 0
42+
assert "Good day Ms. Camila Gutiérrez." in result.output
43+
44+
45+
def test_script():
46+
result = subprocess.run(
47+
["coverage", "run", "--parallel-mode", mod.__file__, "--help"],
48+
stdout=subprocess.PIPE,
49+
stderr=subprocess.PIPE,
50+
encoding="utf-8",
51+
)
52+
assert "Usage" in result.stdout

typer/testing.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class CliRunner(ClickCliRunner):
88
def invoke( # type: ignore
99
self,
1010
app: Typer,
11-
cli: Typer,
1211
args: Optional[Union[str, Iterable[str]]] = None,
1312
input: Optional[Union[bytes, Text, IO[Any]]] = None,
1413
env: Optional[Mapping[str, str]] = None,

0 commit comments

Comments
 (0)