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-09 00:15 +0000

1import subprocess 1acdebfg

2import sys 1acdebfg

3from unittest.mock import patch 1acdebfg

4 

5from typer.testing import CliRunner 1acdebfg

6 

7import docs_src.progressbar.tutorial004_py310 as mod 1acdebfg

8from tests.utils import normalize_rich_output 1acdebfg

9 

10app = mod.app 1acdebfg

11 

12runner = CliRunner() 1acdebfg

13 

14 

15def test_cli(): # Checking only final state of progress bar 1acdebfg

16 consumed = [] 1acdebfg

17 

18 def fake_iterate_user_ids(): 1acdebfg

19 for i in range(100): 1acdebfg

20 consumed.append(i) 1acdebfg

21 yield i 1acdebfg

22 

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) 1acdebfg

31 

32 # Replace all Rich formatting with `*` characters 

33 normalized_output = normalize_rich_output(result.output) 1acdebfg

34 

35 assert result.exit_code == 0 1acdebfg

36 assert len(consumed) == 100 1acdebfg

37 assert mock_sleep.call_count == 100 1acdebfg

38 assert "Processed 100 user IDs." in normalized_output 1acdebfg

39 

40 

41def test_cli_no_mock_generator(): 1acdebfg

42 with ( 1ab

43 patch("time.sleep") as mock_sleep, 

44 ): 

45 result = runner.invoke(app) 1acdebfg

46 

47 # Replace all Rich formatting with `*` characters 

48 normalized_output = normalize_rich_output(result.output) 1acdebfg

49 

50 assert result.exit_code == 0 1acdebfg

51 assert mock_sleep.call_count == 100 1acdebfg

52 assert "Processed 100 user IDs." in normalized_output 1acdebfg

53 

54 

55def test_script(): 1acdebfg

56 result = subprocess.run( 1acdebfg

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

58 capture_output=True, 

59 encoding="utf-8", 

60 ) 

61 assert "Usage" in result.stdout 1acdebfg