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-02-09 12:36 +0000

1import subprocess 1abcdefgh

2import sys 1abcdefgh

3 

4import pytest 1abcdefgh

5import typer.core 1abcdefgh

6from typer.testing import CliRunner 1abcdefgh

7 

8from docs_src.commands.callback import tutorial001_py39 as mod 1abcdefgh

9 

10app = mod.app 1abcdefgh

11 

12runner = CliRunner() 1abcdefgh

13 

14 

15def test_help(): 1abcdefgh

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

17 assert result.exit_code == 0 1abcdefgh

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

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

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

21 

22 

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

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

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

26 assert result.exit_code == 0 1abcdefgh

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

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

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

30 

31 

32def test_create(): 1abcdefgh

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

34 assert result.exit_code == 0 1abcdefgh

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

36 

37 

38def test_create_verbose(): 1abcdefgh

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

40 assert result.exit_code == 0 1abcdefgh

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

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

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

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

45 

46 

47def test_delete(): 1abcdefgh

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

49 assert result.exit_code == 0 1abcdefgh

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

51 

52 

53def test_delete_verbose(): 1abcdefgh

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

55 assert result.exit_code == 0 1abcdefgh

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

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

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

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

60 

61 

62def test_wrong_verbose(): 1abcdefgh

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

64 assert result.exit_code != 0 1abcdefgh

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

66 

67 

68def test_script(): 1abcdefgh

69 result = subprocess.run( 1abcdefgh

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

71 capture_output=True, 

72 encoding="utf-8", 

73 ) 

74 assert "Usage" in result.stdout 1abcdefgh