Skip to content

add test for import pytest error #21472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pythonFiles/tests/pytestadapter/.data/error_pytest_import.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

@pytest.mark.parametrize("num", range(1, 89))
def test_odd_even(num):
assert True
30 changes: 30 additions & 0 deletions pythonFiles/tests/pytestadapter/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,36 @@
from .helpers import TEST_DATA_PATH, runner


def test_import_error(tmp_path):
"""Test pytest discovery on a file that has a pytest marker but does not import pytest.

Copies the contents of a .txt file to a .py file in the temporary directory
to then run pytest discovery on.

The json should still be returned but the errors list should be present.

Keyword arguments:
tmp_path -- pytest fixture that creates a temporary directory.
"""
# Saving some files as .txt to avoid that file displaying a syntax error for
# the extension as a whole. Instead, rename it before running this test
# in order to test the error handling.
file_path = TEST_DATA_PATH / "error_pytest_import.txt"
temp_dir = tmp_path / "temp_data"
temp_dir.mkdir()
p = temp_dir / "error_pytest_import.py"
shutil.copyfile(file_path, p)
actual_list: Optional[List[Dict[str, Any]]] = runner(
["--collect-only", os.fspath(p)]
)
assert actual_list
for actual in actual_list:
assert all(item in actual for item in ("status", "cwd", "error"))
assert actual["status"] == "error"
assert actual["cwd"] == os.fspath(TEST_DATA_PATH)
assert len(actual["error"]) == 2


def test_syntax_error(tmp_path):
"""Test pytest discovery on a file that has a syntax error.

Expand Down
2 changes: 1 addition & 1 deletion pythonFiles/vscode_pytest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def pytest_keyboard_interrupt(excinfo):
Keyword arguments:
excinfo -- the exception information of type ExceptionInfo.
"""
# The function execonly() returns the exception as a string.
# The function exconly() returns the exception as a string.
ERRORS.append(excinfo.exconly())


Expand Down