Coverage for tests / test_tutorial / test_parameter_types / test_enum / test_tutorial004.py: 100%

32 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-02-09 12:36 +0000

1import importlib 1abcdefgh

2import subprocess 1abcdefgh

3import sys 1abcdefgh

4from types import ModuleType 1abcdefgh

5 

6import pytest 1abcdefgh

7from typer.testing import CliRunner 1abcdefgh

8 

9from ....utils import needs_py310 1abcdefgh

10 

11runner = CliRunner() 1abcdefgh

12 

13 

14@pytest.fixture( 1abcdefgh

15 name="mod", 

16 params=[ 

17 pytest.param("tutorial004_py39"), 

18 pytest.param("tutorial004_py310", marks=needs_py310), 

19 pytest.param("tutorial004_an_py39"), 

20 pytest.param("tutorial004_an_py310", marks=needs_py310), 

21 ], 

22) 

23def get_mod(request: pytest.FixtureRequest) -> ModuleType: 1abcdefgh

24 module_name = f"docs_src.parameter_types.enum.{request.param}" 1abcdefgh

25 mod = importlib.import_module(module_name) 1abcdefgh

26 return mod 1abcdefgh

27 

28 

29def test_help(mod: ModuleType): 1abcdefgh

30 result = runner.invoke(mod.app, ["--help"]) 1abcdefgh

31 assert result.exit_code == 0 1abcdefgh

32 assert "--network [simple|conv|lstm]" in result.output.replace(" ", "") 1abcdefgh

33 

34 

35def test_main(mod): 1abcdefgh

36 result = runner.invoke(mod.app, ["--network", "conv"]) 1abcdefgh

37 assert result.exit_code == 0 1abcdefgh

38 assert "Training neural network of type: conv" in result.output 1abcdefgh

39 

40 

41def test_invalid(mod: ModuleType): 1abcdefgh

42 result = runner.invoke(mod.app, ["--network", "capsule"]) 1abcdefgh

43 assert result.exit_code != 0 1abcdefgh

44 assert "Invalid value for '--network'" in result.output 1abcdefgh

45 assert ( 1abcdefgh

46 "invalid choice: capsule. (choose from" in result.output 

47 or "'capsule' is not one of" in result.output 

48 ) 

49 assert "simple" in result.output 1abcdefgh

50 assert "conv" in result.output 1abcdefgh

51 assert "lstm" in result.output 1abcdefgh

52 

53 

54def test_script(mod: ModuleType): 1abcdefgh

55 result = subprocess.run( 1abcdefgh

56 [sys.executable, "-m", "coverage", "run", mod.__file__, "--help"], 

57 capture_output=True, 

58 encoding="utf-8", 

59 ) 

60 assert "Usage" in result.stdout 1abcdefgh