Coverage for tests/test_all.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.3.3, created at 2024-01-02 20:32 +0100

1# SPDX-License-Identifier: MIT 

2# Copyright (c) 2023-2024 Kilian Lackhove 

3 

4import json 

5import subprocess 

6import sys 

7 

8 

9def test_run_and_report(dummy_project_dir, monkeypatch): 

10 monkeypatch.chdir(dummy_project_dir) 

11 

12 coverage_file_path = dummy_project_dir.joinpath(".coverage") 

13 assert not coverage_file_path.is_file() 

14 

15 proc = subprocess.run( 

16 [sys.executable, "-m", "coverage", "run", "main.py"], 

17 cwd=dummy_project_dir, 

18 capture_output=True, 

19 text=True, 

20 check=False, 

21 ) 

22 assert proc.stderr == "" 

23 assert proc.stdout == ( 

24 "Hello, World!\n" 

25 "Variable is set to 'Hello, World!'\n" 

26 "Iteration 1\n" 

27 "Iteration 2\n" 

28 "Iteration 3\n" 

29 "Iteration 4\n" 

30 "Iteration 5\n" 

31 "Hello from a function!\n" 

32 "Current date is: Di 19. Jan 04:14:07 CET 2038\n" 

33 "5 + 3 = 8\n" 

34 "This is a sample file.\n" 

35 "You selected a banana.\n" 

36 ) 

37 assert proc.returncode == 0 

38 

39 assert len(list(dummy_project_dir.glob(".coverage*"))) == 2 

40 

41 proc = subprocess.run( 

42 [sys.executable, "-m", "coverage", "combine"], 

43 cwd=dummy_project_dir, 

44 check=False, 

45 ) 

46 assert proc.returncode == 0 

47 

48 assert len(list(dummy_project_dir.glob(".coverage*"))) == 1 

49 

50 proc = subprocess.run( 

51 [sys.executable, "-m", "coverage", "html"], cwd=dummy_project_dir, check=False 

52 ) 

53 assert proc.returncode == 0 

54 

55 proc = subprocess.run( 

56 [sys.executable, "-m", "coverage", "json"], cwd=dummy_project_dir, check=False 

57 ) 

58 assert proc.returncode == 0 

59 

60 coverage_json = json.loads(dummy_project_dir.joinpath("coverage.json").read_text()) 

61 assert coverage_json["files"]["test.sh"]["excluded_lines"] == [] 

62 assert coverage_json["files"]["test.sh"]["executed_lines"] == [ 

63 12, 

64 15, 

65 18, 

66 19, 

67 25, 

68 26, 

69 31, 

70 34, 

71 37, 

72 38, 

73 41, 

74 42, 

75 45, 

76 46, 

77 47, 

78 48, 

79 51, 

80 52, 

81 57, 

82 ] 

83 assert coverage_json["files"]["test.sh"]["missing_lines"] == [21, 54, 60, 63]