Coverage for cli / tests / test_coverage_parsing.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.14.0, created at 2026-06-19 09:10 +0000

1""" 

2Tests for `_get_coverage_info` - parsing coverage value from an HTML report. 

3""" 

4 

5from pathlib import Path 

6 

7from covered.cli import _get_coverage_info 

8 

9 

10def _write_index(directory: Path, html: str) -> None: 

11 (directory / "index.html").write_text(html) 

12 

13 

14def test_get_coverage_info_returns_none_when_index_missing(tmp_path: Path): 

15 """ 

16 If the directory has no `index.html`, return `None`. 

17 """ 

18 assert _get_coverage_info(tmp_path) is None 

19 

20 

21def test_get_coverage_info_parses_pc_cov_span_template(tmp_path: Path): 

22 """ 

23 Extract the float value from `<span class="pc_cov">…%</span>`. 

24 """ 

25 _write_index(tmp_path, '<span class="pc_cov">87.5%</span>') 

26 

27 assert _get_coverage_info(tmp_path) == 87.5 

28 

29 

30def test_get_coverage_info_returns_none_when_no_pattern_matches(tmp_path: Path): 

31 """ 

32 `index.html` exists but contains no recognised template - return `None`. 

33 """ 

34 _write_index(tmp_path, "<html><body>no coverage info here</body></html>") 

35 

36 assert _get_coverage_info(tmp_path) is None 

37 

38 

39def test_get_coverage_info_handles_decimal_values(tmp_path: Path): 

40 """ 

41 Values like `87.42` are parsed as `float`, not truncated to int. 

42 """ 

43 _write_index(tmp_path, '<span class="pc_cov">87.42%</span>') 

44 

45 result = _get_coverage_info(tmp_path) 

46 

47 assert result == 87.42 

48 assert isinstance(result, float) 

49 

50 

51def test_get_coverage_info_handles_integer_values(tmp_path: Path): 

52 """ 

53 Values like `100` are parsed as `100.0`. 

54 """ 

55 _write_index(tmp_path, '<span class="pc_cov">100%</span>') 

56 

57 result = _get_coverage_info(tmp_path) 

58 

59 assert result == 100.0 

60 assert isinstance(result, float)