forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_coverage.py
91 lines (75 loc) · 3.45 KB
/
test_coverage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import os
import pathlib
import sys
import pytest
sys.path.append(os.fspath(pathlib.Path(__file__).parent))
python_files_path = pathlib.Path(__file__).parent.parent.parent
sys.path.insert(0, os.fspath(python_files_path))
sys.path.insert(0, os.fspath(python_files_path / "lib" / "python"))
from tests.pytestadapter import helpers # noqa: E402
TEST_DATA_PATH = pathlib.Path(__file__).parent / ".data"
def test_basic_coverage():
"""This test runs on a simple django project with three tests, two of which pass and one that fails."""
coverage_ex_folder: pathlib.Path = TEST_DATA_PATH / "coverage_ex"
execution_script: pathlib.Path = python_files_path / "unittestadapter" / "execution.py"
test_ids = [
"test_reverse.TestReverseFunctions.test_reverse_sentence",
"test_reverse.TestReverseFunctions.test_reverse_sentence_error",
"test_reverse.TestReverseFunctions.test_reverse_string",
]
argv = [os.fsdecode(execution_script), "--udiscovery", "-vv", "-s", ".", "-p", "*test*.py"]
argv = argv + test_ids
actual = helpers.runner_with_cwd_env(
argv,
coverage_ex_folder,
{"COVERAGE_ENABLED": os.fspath(coverage_ex_folder), "_TEST_VAR_UNITTEST": "True"},
)
assert actual
coverage = actual[-1]
assert coverage
results = coverage["result"]
assert results
assert len(results) == 3
focal_function_coverage = results.get(os.fspath(TEST_DATA_PATH / "coverage_ex" / "reverse.py"))
assert focal_function_coverage
assert focal_function_coverage.get("lines_covered") is not None
assert focal_function_coverage.get("lines_missed") is not None
assert set(focal_function_coverage.get("lines_covered")) == {4, 5, 7, 9, 10, 11, 12, 13, 14}
assert set(focal_function_coverage.get("lines_missed")) == {6}
@pytest.mark.timeout(30)
def test_basic_django_coverage():
"""This test validates that the coverage is correctly calculated for a Django project."""
data_path: pathlib.Path = TEST_DATA_PATH / "simple_django"
manage_py_path: str = os.fsdecode(data_path / "manage.py")
execution_script: pathlib.Path = python_files_path / "unittestadapter" / "execution.py"
test_ids = [
"polls.tests.QuestionModelTests.test_was_published_recently_with_future_question",
"polls.tests.QuestionModelTests.test_was_published_recently_with_future_question_2",
"polls.tests.QuestionModelTests.test_question_creation_and_retrieval",
]
script_str = os.fsdecode(execution_script)
actual = helpers.runner_with_cwd_env(
[script_str, "--udiscovery", "-p", "*test*.py", *test_ids],
data_path,
{
"MANAGE_PY_PATH": manage_py_path,
"_TEST_VAR_UNITTEST": "True",
"COVERAGE_ENABLED": os.fspath(data_path),
},
)
assert actual
coverage = actual[-1]
assert coverage
results = coverage["result"]
assert results
assert len(results) == 15
polls_views_coverage = results.get(str(data_path / "polls" / "views.py"))
assert polls_views_coverage
assert polls_views_coverage.get("lines_covered") is not None
assert polls_views_coverage.get("lines_missed") is not None
assert set(polls_views_coverage.get("lines_covered")) == {3, 4, 6}
assert set(polls_views_coverage.get("lines_missed")) == {7}