forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of Test Coverage (microsoft#24118)
fixes microsoft#22671
- Loading branch information
1 parent
717e518
commit 8268131
Showing
30 changed files
with
895 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# List of requirements for functional tests | ||
versioneer | ||
numpy | ||
pytest | ||
pytest-cov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
python_files/tests/pytestadapter/.data/coverage_gen/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. |
19 changes: 19 additions & 0 deletions
19
python_files/tests/pytestadapter/.data/coverage_gen/reverse.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
def reverse_string(s): | ||
if s is None or s == "": | ||
return "Error: Input is None" | ||
return s[::-1] | ||
|
||
def reverse_sentence(sentence): | ||
if sentence is None or sentence == "": | ||
return "Error: Input is None" | ||
words = sentence.split() | ||
reversed_words = [reverse_string(word) for word in words] | ||
return " ".join(reversed_words) | ||
|
||
# Example usage | ||
if __name__ == "__main__": | ||
sample_string = "hello" | ||
print(reverse_string(sample_string)) # Output: "olleh" |
28 changes: 28 additions & 0 deletions
28
python_files/tests/pytestadapter/.data/coverage_gen/test_reverse.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
from .reverse import reverse_sentence, reverse_string | ||
|
||
|
||
def test_reverse_sentence(): | ||
""" | ||
Tests the reverse_sentence function to ensure it correctly reverses each word in a sentence. | ||
Test cases: | ||
- "hello world" should be reversed to "olleh dlrow" | ||
- "Python is fun" should be reversed to "nohtyP si nuf" | ||
- "a b c" should remain "a b c" as each character is a single word | ||
""" | ||
assert reverse_sentence("hello world") == "olleh dlrow" | ||
assert reverse_sentence("Python is fun") == "nohtyP si nuf" | ||
assert reverse_sentence("a b c") == "a b c" | ||
|
||
def test_reverse_sentence_error(): | ||
assert reverse_sentence("") == "Error: Input is None" | ||
assert reverse_sentence(None) == "Error: Input is None" | ||
|
||
|
||
def test_reverse_string(): | ||
assert reverse_string("hello") == "olleh" | ||
assert reverse_string("Python") == "nohtyP" | ||
# this test specifically does not cover the error cases |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
import os | ||
import pathlib | ||
import sys | ||
|
||
script_dir = pathlib.Path(__file__).parent.parent | ||
sys.path.append(os.fspath(script_dir)) | ||
|
||
from .helpers import ( # noqa: E402 | ||
TEST_DATA_PATH, | ||
runner_with_cwd_env, | ||
) | ||
|
||
|
||
def test_simple_pytest_coverage(): | ||
""" | ||
Test coverage payload is correct for simple pytest example. Output of coverage run is below. | ||
Name Stmts Miss Branch BrPart Cover | ||
--------------------------------------------------- | ||
__init__.py 0 0 0 0 100% | ||
reverse.py 13 3 8 2 76% | ||
test_reverse.py 11 0 0 0 100% | ||
--------------------------------------------------- | ||
TOTAL 24 3 8 2 84% | ||
""" | ||
args = [] | ||
env_add = {"COVERAGE_ENABLED": "True"} | ||
cov_folder_path = TEST_DATA_PATH / "coverage_gen" | ||
actual = runner_with_cwd_env(args, cov_folder_path, env_add) | ||
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_gen" / "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, 17} | ||
assert set(focal_function_coverage.get("lines_missed")) == {18, 19, 6} | ||
assert ( | ||
focal_function_coverage.get("executed_branches") > 0 | ||
), "executed_branches are a number greater than 0." | ||
assert ( | ||
focal_function_coverage.get("total_branches") > 0 | ||
), "total_branches are a number greater than 0." |
2 changes: 2 additions & 0 deletions
2
python_files/tests/unittestadapter/.data/coverage_ex/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. |
14 changes: 14 additions & 0 deletions
14
python_files/tests/unittestadapter/.data/coverage_ex/reverse.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
def reverse_string(s): | ||
if s is None or s == "": | ||
return "Error: Input is None" | ||
return s[::-1] | ||
|
||
def reverse_sentence(sentence): | ||
if sentence is None or sentence == "": | ||
return "Error: Input is None" | ||
words = sentence.split() | ||
reversed_words = [reverse_string(word) for word in words] | ||
return " ".join(reversed_words) |
32 changes: 32 additions & 0 deletions
32
python_files/tests/unittestadapter/.data/coverage_ex/test_reverse.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
import unittest | ||
from reverse import reverse_sentence, reverse_string | ||
|
||
class TestReverseFunctions(unittest.TestCase): | ||
|
||
def test_reverse_sentence(self): | ||
""" | ||
Tests the reverse_sentence function to ensure it correctly reverses each word in a sentence. | ||
Test cases: | ||
- "hello world" should be reversed to "olleh dlrow" | ||
- "Python is fun" should be reversed to "nohtyP si nuf" | ||
- "a b c" should remain "a b c" as each character is a single word | ||
""" | ||
self.assertEqual(reverse_sentence("hello world"), "olleh dlrow") | ||
self.assertEqual(reverse_sentence("Python is fun"), "nohtyP si nuf") | ||
self.assertEqual(reverse_sentence("a b c"), "a b c") | ||
|
||
def test_reverse_sentence_error(self): | ||
self.assertEqual(reverse_sentence(""), "Error: Input is None") | ||
self.assertEqual(reverse_sentence(None), "Error: Input is None") | ||
|
||
def test_reverse_string(self): | ||
self.assertEqual(reverse_string("hello"), "olleh") | ||
self.assertEqual(reverse_string("Python"), "nohtyP") | ||
# this test specifically does not cover the error cases | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# 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 | ||
|
||
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} | ||
assert ( | ||
focal_function_coverage.get("executed_branches") > 0 | ||
), "executed_branches are a number greater than 0." | ||
assert ( | ||
focal_function_coverage.get("total_branches") > 0 | ||
), "total_branches are a number greater than 0." |
Oops, something went wrong.