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

19 statements  

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

1import subprocess 1abcdefghij

2import sys 1abcdefghij

3from unittest.mock import patch 1abcdefghij

4 

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

6from typer.testing import CliRunner 1abcdefghij

7 

8import docs_src.printing.tutorial001 as mod 1abcdefghij

9from tests.utils import normalize_rich_output 1abcdefghij

10 

11app = mod.app 1abcdefghij

12 

13runner = CliRunner() 1abcdefghij

14 

15 

16def test_cli(): 1abcdefghij

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

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

19 result = runner.invoke(app) 1abcdefghij

20 

21 assert result.exit_code == 0 1abcdefghij

22 

23 # Replace all Rich formatting with `*` characters 

24 normalized_output = normalize_rich_output(result.output, squash_whitespaces=False) 1abcdefghij

25 

26 assert ( 1abcdefghij

27 "Here's the data\n" 

28 "*{*\n" 

29 " *'name'*: *'Rick'*,\n" 

30 " *'age'*: *42*,\n" 

31 " *'items'*: *[*{*'name'*: *'Portal Gun'*}*, *{*'name'*: *'Plumbus'*}*]*,\n" 

32 " *'active'*: *True*,\n" 

33 " *'affiliation'*: *None*\n" 

34 "*}*\n" 

35 ) in normalized_output 

36 

37 

38def test_script(): 1abcdefghij

39 result = subprocess.run( 1abcdefghij

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

41 capture_output=True, 

42 encoding="utf-8", 

43 ) 

44 assert "Usage" in result.stdout 1abcdefghij