Coverage for tests / test_tutorial / test_subcommands / test_tutorial001.py: 100%

66 statements  

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

1import os 1abcdefgh

2import subprocess 1abcdefgh

3import sys 1abcdefgh

4 

5import pytest 1abcdefgh

6from typer.testing import CliRunner 1abcdefgh

7 

8from docs_src.subcommands import tutorial001_py39 1abcdefgh

9 

10runner = CliRunner() 1abcdefgh

11 

12 

13@pytest.fixture() 1abcdefgh

14def mod(monkeypatch): 1abcdefgh

15 with monkeypatch.context(): 1abcdefgh

16 monkeypatch.syspath_prepend(list(tutorial001_py39.__path__)[0]) 1abcdefgh

17 from docs_src.subcommands.tutorial001_py39 import main 1abcdefgh

18 

19 return main 1abcdefgh

20 

21 

22@pytest.fixture() 1abcdefgh

23def app(mod): 1abcdefgh

24 return mod.app 1abcdefgh

25 

26 

27def test_help(app): 1abcdefgh

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

29 assert result.exit_code == 0 1abcdefgh

30 assert "[OPTIONS] COMMAND [ARGS]..." in result.output 1abcdefgh

31 assert "Commands" in result.output 1abcdefgh

32 assert "items" in result.output 1abcdefgh

33 assert "users" in result.output 1abcdefgh

34 

35 

36def test_help_items(app): 1abcdefgh

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

38 assert result.exit_code == 0 1abcdefgh

39 assert "[OPTIONS] COMMAND [ARGS]..." in result.output 1abcdefgh

40 assert "Commands" in result.output 1abcdefgh

41 assert "create" in result.output 1abcdefgh

42 assert "delete" in result.output 1abcdefgh

43 assert "sell" in result.output 1abcdefgh

44 

45 

46def test_items_create(app): 1abcdefgh

47 result = runner.invoke(app, ["items", "create", "Wand"]) 1abcdefgh

48 assert result.exit_code == 0 1abcdefgh

49 assert "Creating item: Wand" in result.output 1abcdefgh

50 

51 

52def test_items_sell(app): 1abcdefgh

53 result = runner.invoke(app, ["items", "sell", "Vase"]) 1abcdefgh

54 assert result.exit_code == 0 1abcdefgh

55 assert "Selling item: Vase" in result.output 1abcdefgh

56 

57 

58def test_items_delete(app): 1abcdefgh

59 result = runner.invoke(app, ["items", "delete", "Vase"]) 1abcdefgh

60 assert result.exit_code == 0 1abcdefgh

61 assert "Deleting item: Vase" in result.output 1abcdefgh

62 

63 

64def test_help_users(app): 1abcdefgh

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

66 assert result.exit_code == 0 1abcdefgh

67 assert "[OPTIONS] COMMAND [ARGS]..." in result.output 1abcdefgh

68 assert "Commands" in result.output 1abcdefgh

69 assert "create" in result.output 1abcdefgh

70 assert "delete" in result.output 1abcdefgh

71 assert "sell" not in result.output 1abcdefgh

72 

73 

74def test_users_create(app): 1abcdefgh

75 result = runner.invoke(app, ["users", "create", "Camila"]) 1abcdefgh

76 assert result.exit_code == 0 1abcdefgh

77 assert "Creating user: Camila" in result.output 1abcdefgh

78 

79 

80def test_users_delete(app): 1abcdefgh

81 result = runner.invoke(app, ["users", "delete", "Camila"]) 1abcdefgh

82 assert result.exit_code == 0 1abcdefgh

83 assert "Deleting user: Camila" in result.output 1abcdefgh

84 

85 

86def test_scripts(mod): 1abcdefgh

87 from docs_src.subcommands.tutorial001_py39 import items, users 1abcdefgh

88 

89 env = os.environ.copy() 1abcdefgh

90 env["PYTHONPATH"] = ":".join(list(tutorial001_py39.__path__)) 1abcdefgh

91 

92 for module in [mod, items, users]: 1abcdefgh

93 result = subprocess.run( 1abcdefgh

94 [sys.executable, "-m", "coverage", "run", module.__file__, "--help"], 

95 capture_output=True, 

96 encoding="utf-8", 

97 env=env, 

98 ) 

99 assert "Usage" in result.stdout 1abcdefgh