|
| 1 | +import importlib |
1 | 2 | import subprocess |
2 | 3 | import sys |
| 4 | +from types import ModuleType |
3 | 5 |
|
| 6 | +import pytest |
4 | 7 | from typer.testing import CliRunner |
5 | 8 |
|
6 | | -from docs_src.multiple_values.arguments_with_multiple_values import ( |
7 | | - tutorial002_py39 as mod, |
8 | | -) |
9 | | - |
10 | 9 | runner = CliRunner() |
11 | | -app = mod.app |
12 | 10 |
|
13 | 11 |
|
14 | | -def test_help(): |
15 | | - result = runner.invoke(app, ["--help"]) |
| 12 | +@pytest.fixture( |
| 13 | + name="mod", |
| 14 | + params=[ |
| 15 | + pytest.param("tutorial002_py39"), |
| 16 | + pytest.param("tutorial002_an_py39"), |
| 17 | + ], |
| 18 | +) |
| 19 | +def get_mod(request: pytest.FixtureRequest) -> ModuleType: |
| 20 | + module_name = ( |
| 21 | + f"docs_src.multiple_values.arguments_with_multiple_values.{request.param}" |
| 22 | + ) |
| 23 | + mod = importlib.import_module(module_name) |
| 24 | + return mod |
| 25 | + |
| 26 | + |
| 27 | +def test_help(mod: ModuleType): |
| 28 | + result = runner.invoke(mod.app, ["--help"]) |
16 | 29 | assert result.exit_code == 0 |
17 | 30 | assert "[OPTIONS] [NAMES]..." in result.output |
18 | 31 | assert "Arguments" in result.output |
19 | 32 | assert "[default: Harry, Hermione, Ron]" in result.output |
20 | 33 |
|
21 | 34 |
|
22 | | -def test_defaults(): |
23 | | - result = runner.invoke(app) |
| 35 | +def test_defaults(mod: ModuleType): |
| 36 | + result = runner.invoke(mod.app) |
24 | 37 | assert result.exit_code == 0 |
25 | 38 | assert "Hello Harry" in result.output |
26 | 39 | assert "Hello Hermione" in result.output |
27 | 40 | assert "Hello Ron" in result.output |
28 | 41 |
|
29 | 42 |
|
30 | | -def test_invalid_args(): |
31 | | - result = runner.invoke(app, ["Draco", "Hagrid"]) |
| 43 | +def test_invalid_args(mod: ModuleType): |
| 44 | + result = runner.invoke(mod.app, ["Draco", "Hagrid"]) |
32 | 45 | assert result.exit_code != 0 |
33 | 46 | assert "Argument 'names' takes 3 values" in result.output |
34 | 47 |
|
35 | 48 |
|
36 | | -def test_valid_args(): |
37 | | - result = runner.invoke(app, ["Draco", "Hagrid", "Dobby"]) |
| 49 | +def test_valid_args(mod: ModuleType): |
| 50 | + result = runner.invoke(mod.app, ["Draco", "Hagrid", "Dobby"]) |
38 | 51 | assert result.exit_code == 0 |
39 | 52 | assert "Hello Draco" in result.stdout |
40 | 53 | assert "Hello Hagrid" in result.stdout |
41 | 54 | assert "Hello Dobby" in result.stdout |
42 | 55 |
|
43 | 56 |
|
44 | | -def test_script(): |
| 57 | +def test_script(mod: ModuleType): |
45 | 58 | result = subprocess.run( |
46 | 59 | [sys.executable, "-m", "coverage", "run", mod.__file__, "--help"], |
47 | 60 | capture_output=True, |
|
0 commit comments