Coverage for tests / test_tutorial / test_commands / test_callback / test_tutorial001.py: 100%

50 statements  

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

1import subprocess 1abcdefghij

2import sys 1abcdefghij

3 

4import pytest 1abcdefghij

5import typer.core 1abcdefghij

6from typer.testing import CliRunner 1abcdefghij

7 

8from docs_src.commands.callback import tutorial001 as mod 1abcdefghij

9 

10app = mod.app 1abcdefghij

11 

12runner = CliRunner() 1abcdefghij

13 

14 

15def test_help(): 1abcdefghij

16 result = runner.invoke(app, ["--help"]) 1abcdefghij

17 assert result.exit_code == 0 1abcdefghij

18 assert "Manage users in the awesome CLI app." in result.output 1abcdefghij

19 assert "--verbose" in result.output 1abcdefghij

20 assert "--no-verbose" in result.output 1abcdefghij

21 

22 

23def test_help_no_rich(monkeypatch: pytest.MonkeyPatch): 1abcdefghij

24 monkeypatch.setattr(typer.core, "HAS_RICH", False) 1abcdefghij

25 result = runner.invoke(app, ["--help"]) 1abcdefghij

26 assert result.exit_code == 0 1abcdefghij

27 assert "Manage users in the awesome CLI app." in result.output 1abcdefghij

28 assert "--verbose" in result.output 1abcdefghij

29 assert "--no-verbose" in result.output 1abcdefghij

30 

31 

32def test_create(): 1abcdefghij

33 result = runner.invoke(app, ["create", "Camila"]) 1abcdefghij

34 assert result.exit_code == 0 1abcdefghij

35 assert "Creating user: Camila" in result.output 1abcdefghij

36 

37 

38def test_create_verbose(): 1abcdefghij

39 result = runner.invoke(app, ["--verbose", "create", "Camila"]) 1abcdefghij

40 assert result.exit_code == 0 1abcdefghij

41 assert "Will write verbose output" in result.output 1abcdefghij

42 assert "About to create a user" in result.output 1abcdefghij

43 assert "Creating user: Camila" in result.output 1abcdefghij

44 assert "Just created a user" in result.output 1abcdefghij

45 

46 

47def test_delete(): 1abcdefghij

48 result = runner.invoke(app, ["delete", "Camila"]) 1abcdefghij

49 assert result.exit_code == 0 1abcdefghij

50 assert "Deleting user: Camila" in result.output 1abcdefghij

51 

52 

53def test_delete_verbose(): 1abcdefghij

54 result = runner.invoke(app, ["--verbose", "delete", "Camila"]) 1abcdefghij

55 assert result.exit_code == 0 1abcdefghij

56 assert "Will write verbose output" in result.output 1abcdefghij

57 assert "About to delete a user" in result.output 1abcdefghij

58 assert "Deleting user: Camila" in result.output 1abcdefghij

59 assert "Just deleted a user" in result.output 1abcdefghij

60 

61 

62def test_wrong_verbose(): 1abcdefghij

63 result = runner.invoke(app, ["delete", "--verbose", "Camila"]) 1abcdefghij

64 assert result.exit_code != 0 1abcdefghij

65 assert "No such option: --verbose" in result.output 1abcdefghij

66 

67 

68def test_script(): 1abcdefghij

69 result = subprocess.run( 1abcdefghij

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

71 capture_output=True, 

72 encoding="utf-8", 

73 ) 

74 assert "Usage" in result.stdout 1abcdefghij