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