Coverage for tests / test_tutorial / test_options / test_version / test_tutorial003.py: 100%

34 statements  

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

1import importlib 1abcdefghij

2import os 1abcdefghij

3import subprocess 1abcdefghij

4import sys 1abcdefghij

5from pathlib import Path 1abcdefghij

6from types import ModuleType 1abcdefghij

7 

8import pytest 1abcdefghij

9from typer.testing import CliRunner 1abcdefghij

10 

11runner = CliRunner() 1abcdefghij

12 

13 

14@pytest.fixture( 1abcdefghij

15 name="mod", 

16 params=[ 

17 pytest.param("tutorial003_non_an"), 

18 pytest.param("tutorial003"), 

19 ], 

20) 

21def get_mod(request: pytest.FixtureRequest) -> ModuleType: 1abcdefghij

22 module_name = f"docs_src.options.version.{request.param}" 1abcdefghij

23 mod = importlib.import_module(module_name) 1abcdefghij

24 return mod 1abcdefghij

25 

26 

27def test_1(mod: ModuleType): 1abcdefghij

28 result = runner.invoke(mod.app, ["--name", "Rick", "--version"]) 1abcdefghij

29 assert result.exit_code == 0 1abcdefghij

30 assert "Awesome CLI Version: 0.1.0" in result.output 1abcdefghij

31 

32 

33def test_2(mod: ModuleType): 1abcdefghij

34 result = runner.invoke(mod.app, ["--name", "rick"]) 1abcdefghij

35 assert result.exit_code != 0 1abcdefghij

36 assert "Invalid value for '--name'" in result.output 1abcdefghij

37 assert "Only Camila is allowed" in result.output 1abcdefghij

38 

39 

40def test_3(mod: ModuleType): 1abcdefghij

41 result = runner.invoke(mod.app, ["--name", "Camila"]) 1abcdefghij

42 assert result.exit_code == 0 1abcdefghij

43 assert "Hello Camila" in result.output 1abcdefghij

44 

45 

46def test_script(mod: ModuleType): 1abcdefghij

47 result = subprocess.run( 1abcdefghij

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

49 capture_output=True, 

50 encoding="utf-8", 

51 ) 

52 assert "Usage" in result.stdout 1abcdefghij

53 

54 

55def test_completion(mod: ModuleType): 1abcdefghij

56 file_name = Path(mod.__file__).name 1abcdefghij

57 result = subprocess.run( 1abcdefghij

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

59 capture_output=True, 

60 encoding="utf-8", 

61 env={ 

62 **os.environ, 

63 f"_{file_name.upper()}_COMPLETE": "complete_bash", 

64 "COMP_WORDS": f"{file_name} --name Rick --v", 

65 "COMP_CWORD": "3", 

66 }, 

67 ) 

68 assert "--version" in result.stdout 1abcdefghij