Coverage for tests / test_tutorial / test_progressbar / test_tutorial004.py: 100%
31 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-11-18 11:12 +0000
« 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
5from typer.testing import CliRunner 1abcdefghij
7import docs_src.progressbar.tutorial004 as mod 1abcdefghij
8from tests.utils import normalize_rich_output 1abcdefghij
10app = mod.app 1abcdefghij
12runner = CliRunner() 1abcdefghij
15def test_cli(): # Checking only final state of progress bar 1abcdefghij
16 consumed = [] 1abcdefghij
18 def fake_iterate_user_ids(): 1abcdefghij
19 for i in range(100): 1abcdefghij
20 consumed.append(i) 1abcdefghij
21 yield i 1abcdefghij
23 with ( 1a
24 patch("time.sleep") as mock_sleep,
25 patch(
26 "docs_src.progressbar.tutorial004.iterate_user_ids",
27 side_effect=fake_iterate_user_ids,
28 ),
29 ):
30 result = runner.invoke(app) 1abcdefghij
32 # Replace all Rich formatting with `*` characters
33 normalized_output = normalize_rich_output(result.output) 1abcdefghij
35 assert result.exit_code == 0 1abcdefghij
36 assert len(consumed) == 100 1abcdefghij
37 assert mock_sleep.call_count == 100 1abcdefghij
38 assert "Processed 100 user IDs." in normalized_output 1abcdefghij
41def test_cli_no_mock_generator(): 1abcdefghij
42 with ( 1a
43 patch("time.sleep") as mock_sleep,
44 ):
45 result = runner.invoke(app) 1abcdefghij
47 # Replace all Rich formatting with `*` characters
48 normalized_output = normalize_rich_output(result.output) 1abcdefghij
50 assert result.exit_code == 0 1abcdefghij
51 assert mock_sleep.call_count == 100 1abcdefghij
52 assert "Processed 100 user IDs." in normalized_output 1abcdefghij
55def test_script(): 1abcdefghij
56 result = subprocess.run( 1abcdefghij
57 [sys.executable, "-m", "coverage", "run", mod.__file__, "--help"],
58 capture_output=True,
59 encoding="utf-8",
60 )
61 assert "Usage" in result.stdout 1abcdefghij