Coverage for tests / test_tutorial / test_printing / test_tutorial002.py: 100%

23 statements  

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

1import subprocess 1abcdefgh

2import sys 1abcdefgh

3from unittest.mock import patch 1abcdefgh

4 

5from rich.console import Console # noqa: TID251 1abcdefgh

6from typer.testing import CliRunner 1abcdefgh

7 

8import docs_src.printing.tutorial002_py39 as mod 1abcdefgh

9from tests.utils import normalize_rich_output 1abcdefgh

10 

11app = mod.app 1abcdefgh

12 

13runner = CliRunner() 1abcdefgh

14 

15 

16def test_cli(): 1abcdefgh

17 console = Console(force_terminal=True, width=100) 1abcdefgh

18 with patch("rich.get_console", return_value=console): 1abcdefgh

19 result = runner.invoke(app) 1abcdefgh

20 

21 # Replace all Rich formatting with `*` characters 

22 normalized_output = normalize_rich_output(result.output) 1abcdefgh

23 

24 assert result.exit_code == 0 1abcdefgh

25 assert "*Alert!* *Portal gun* shooting! *" in normalized_output 1abcdefgh

26 

27 

28def test_cli_without_formatting(): 1abcdefgh

29 result = runner.invoke(app) 1abcdefgh

30 

31 assert result.exit_code == 0 1abcdefgh

32 assert "Alert! Portal gun shooting! 💥" in result.output 1abcdefgh

33 

34 

35def test_script(): 1abcdefgh

36 result = subprocess.run( 1abcdefgh

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

38 capture_output=True, 

39 encoding="utf-8", 

40 ) 

41 assert "Usage" in result.stdout 1abcdefgh