Skip to content

Commit

Permalink
fix for execution
Browse files Browse the repository at this point in the history
  • Loading branch information
eleanorjboyd committed Jul 24, 2023
1 parent 85f2f8e commit 3da2ee7
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 12 deletions.
13 changes: 13 additions & 0 deletions pythonFiles/tests/pytestadapter/expected_execution_test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,16 @@
"subtest": None,
},
}

# This is the expected output for the root folder with the config file referenced.
# └── test_a.py
# └── test_a_function: success
config_file_pytest_expected_execution_output = {
"tests/test_a.py::test_a_function": {
"test": "tests/test_a.py::test_a_function",
"outcome": "success",
"message": None,
"traceback": None,
"subtest": None,
}
}
2 changes: 0 additions & 2 deletions pythonFiles/tests/pytestadapter/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@ def test_pytest_root_dir():
[
"--collect-only",
rd,
# "-c",
# "tests/pytest.ini",
],
TEST_DATA_PATH / "root",
)
Expand Down
42 changes: 41 additions & 1 deletion pythonFiles/tests/pytestadapter/test_execution.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,52 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import json
import os
import shutil

import pytest
from tests.pytestadapter import expected_execution_test_output

from .helpers import TEST_DATA_PATH, runner
from .helpers import TEST_DATA_PATH, runner, runner_with_cwd


def test_config_file():
"""Test pytest execution when a config file is specified."""
args = ["-c", "tests/pytest.ini", "tests/test_a.py::test_a_function"]
new_cwd = TEST_DATA_PATH / "root"
actual = runner_with_cwd(args, new_cwd)
expected_const = (
expected_execution_test_output.config_file_pytest_expected_execution_output
)
assert actual
assert len(actual) == len(expected_const)
actual_result_dict = dict()
for a in actual:
assert all(item in a for item in ("status", "cwd", "result"))
assert a["status"] == "success"
assert a["cwd"] == os.fspath(new_cwd)
actual_result_dict.update(a["result"])
assert actual_result_dict == expected_const


def test_rootdir_specified():
"""Test pytest execution when a --rootdir is specified."""
rd = f"--rootdir={TEST_DATA_PATH / 'root' / 'tests'}"
args = [rd, "tests/test_a.py::test_a_function"]
new_cwd = TEST_DATA_PATH / "root"
actual = runner_with_cwd(args, new_cwd)
expected_const = (
expected_execution_test_output.config_file_pytest_expected_execution_output
)
assert actual
assert len(actual) == len(expected_const)
actual_result_dict = dict()
for a in actual:
assert all(item in a for item in ("status", "cwd", "result"))
assert a["status"] == "success"
assert a["cwd"] == os.fspath(new_cwd)
actual_result_dict.update(a["result"])
assert actual_result_dict == expected_const


def test_syntax_error_execution(tmp_path):
Expand Down
23 changes: 14 additions & 9 deletions pythonFiles/vscode_pytest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def pytest_exception_interact(node, call, report):
report_value = "error"
if call.excinfo.typename == "AssertionError":
report_value = "failure"
node_id = str(node.nodeid)
node_id = get_workspace_node_id(str(node.nodeid))
if node_id not in collected_tests_so_far:
collected_tests_so_far.append(node_id)
item_result = create_test_outcome(
Expand All @@ -106,6 +106,14 @@ def pytest_exception_interact(node, call, report):
)


def get_workspace_node_id(testid: str):
id = testid
global RELATIVE_INVOCATION_PATH
if RELATIVE_INVOCATION_PATH:
id = str(pathlib.Path(RELATIVE_INVOCATION_PATH) / testid)
return id


def pytest_keyboard_interrupt(excinfo):
"""A pytest hook that is called when a keyboard interrupt is raised.
Expand All @@ -130,15 +138,15 @@ class TestOutcome(Dict):


def create_test_outcome(
test: str,
testid: str,
outcome: str,
message: Union[str, None],
traceback: Union[str, None],
subtype: Optional[str] = None,
) -> TestOutcome:
"""A function that creates a TestOutcome object."""
return TestOutcome(
test=test,
test=testid,
outcome=outcome,
message=message,
traceback=traceback, # TODO: traceback
Expand Down Expand Up @@ -193,7 +201,7 @@ def pytest_report_teststatus(report, config):
elif report.failed:
report_value = "failure"
message = report.longreprtext
node_id = str(report.nodeid)
node_id = get_workspace_node_id(str(report.nodeid))
if node_id not in collected_tests_so_far:
collected_tests_so_far.append(node_id)
item_result = create_test_outcome(
Expand Down Expand Up @@ -222,7 +230,7 @@ def pytest_report_teststatus(report, config):
def pytest_runtest_protocol(item, nextitem):
skipped = check_skipped_wrapper(item)
if skipped:
node_id = str(item.nodeid)
node_id = get_workspace_node_id(str(item.nodeid))
report_value = "skipped"
cwd = pathlib.Path.cwd()
if node_id not in collected_tests_so_far:
Expand Down Expand Up @@ -480,10 +488,7 @@ def create_test_node(
test_case_loc: str = (
str(test_case.location[1] + 1) if (test_case.location[1] is not None) else ""
)
id = test_case.nodeid
global RELATIVE_INVOCATION_PATH
if RELATIVE_INVOCATION_PATH:
id = str(pathlib.Path(RELATIVE_INVOCATION_PATH) / test_case.nodeid)
id = get_workspace_node_id(test_case.nodeid)
return {
"name": test_case.name,
"path": get_node_path(test_case),
Expand Down

0 comments on commit 3da2ee7

Please sign in to comment.