Skip to content

Commit

Permalink
fix unittests python tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eleanorjboyd committed Jan 22, 2024
1 parent 854a3ad commit 7c2a66c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 30 deletions.
12 changes: 4 additions & 8 deletions pythonFiles/tests/unittestadapter/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ def test_simple_discovery() -> None:
"id_": start_dir,
}

uuid = "some-uuid"
actual = discover_tests(start_dir, pattern, None, uuid)
actual = discover_tests(start_dir, pattern, None)

assert actual["status"] == "success"
assert is_same_tree(actual.get("tests"), expected)
Expand All @@ -140,8 +139,7 @@ def test_empty_discovery() -> None:
start_dir = os.fsdecode(TEST_DATA_PATH)
pattern = "discovery_empty*"

uuid = "some-uuid"
actual = discover_tests(start_dir, pattern, None, uuid)
actual = discover_tests(start_dir, pattern, None)

assert actual["status"] == "success"
assert "tests" in actual
Expand Down Expand Up @@ -206,8 +204,7 @@ def test_error_discovery() -> None:
"id_": start_dir,
}

uuid = "some-uuid"
actual = discover_tests(start_dir, pattern, None, uuid)
actual = discover_tests(start_dir, pattern, None)

assert actual["status"] == "error"
assert is_same_tree(expected, actual.get("tests"))
Expand All @@ -221,8 +218,7 @@ def test_unit_skip() -> None:
start_dir = os.fsdecode(TEST_DATA_PATH / "unittest_skip")
pattern = "unittest_*"

uuid = "some-uuid"
actual = discover_tests(start_dir, pattern, None, uuid)
actual = discover_tests(start_dir, pattern, None)

assert actual["status"] == "success"
assert "tests" in actual
Expand Down
57 changes: 35 additions & 22 deletions pythonFiles/tests/unittestadapter/test_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import os
import pathlib
import sys
from unittest.mock import patch

import pytest

script_dir = pathlib.Path(__file__).parent.parent
sys.path.insert(0, os.fspath(script_dir / "lib" / "python"))

from unittestadapter.execution import run_tests
from unittestadapter.execution import run_tests, send_run_data

TEST_DATA_PATH = pathlib.Path(__file__).parent / ".data"

Expand All @@ -22,7 +23,7 @@ def test_no_ids_run() -> None:
start_dir: str = os.fspath(TEST_DATA_PATH)
testids = []
pattern = "discovery_simple*"
actual = run_tests(start_dir, testids, pattern, None, "fake-uuid", 1, None)
actual = run_tests(start_dir, testids, pattern, None, 1, None)
assert actual
assert all(item in actual for item in ("cwd", "status"))
assert actual["status"] == "success"
Expand All @@ -33,49 +34,56 @@ def test_no_ids_run() -> None:
raise AssertionError("actual['result'] is None")


def test_single_ids_run() -> None:
@pytest.fixture
def mock_send_run_data():
with patch("unittestadapter.execution.send_run_data") as mock:
yield mock


def test_single_ids_run(mock_send_run_data):
"""This test runs on a single test_id, therefore it should return
a dict with a single key-value pair for the result.
This single test passes so the outcome should be 'success'.
"""
id = "discovery_simple.DiscoverySimple.test_one"
os.environ["TEST_RUN_PIPE"] = "fake"
actual = run_tests(
os.fspath(TEST_DATA_PATH),
[id],
"discovery_simple*",
None,
"fake-uuid",
1,
None,
)
assert actual
assert all(item in actual for item in ("cwd", "status"))
assert actual["status"] == "success"
assert actual["cwd"] == os.fspath(TEST_DATA_PATH)
assert actual["result"] is not None
result = actual["result"]
assert len(result) == 1
assert id in result
id_result = result[id]

# Access the arguments
args, _ = mock_send_run_data.call_args
actual_result = args[0] # first argument is the result

assert actual_result
actual_result = actual["result"]
assert len(actual_result) == 1
assert id in actual_result
id_result = actual_result[id]
assert id_result is not None
assert "outcome" in id_result
assert id_result["outcome"] == "success"


def test_subtest_run() -> None:
def test_subtest_run(mock_send_run_data) -> None:
"""This test runs on a the test_subtest which has a single method, test_even,
that uses unittest subtest.
The actual result of run should return a dict payload with 6 entry for the 6 subtests.
"""
id = "test_subtest.NumbersTest.test_even"
os.environ["TEST_RUN_PIPE"] = "fake"
actual = run_tests(
os.fspath(TEST_DATA_PATH),
[id],
"test_subtest.py",
None,
"fake-uuid",
1,
None,
)
Expand Down Expand Up @@ -161,7 +169,9 @@ def test_subtest_run() -> None:
),
],
)
def test_multiple_ids_run(test_ids, pattern, cwd, expected_outcome) -> None:
def test_multiple_ids_run(
mock_send_run_data, test_ids, pattern, cwd, expected_outcome
) -> None:
"""
The following are all successful tests of different formats.
Expand All @@ -174,7 +184,8 @@ def test_multiple_ids_run(test_ids, pattern, cwd, expected_outcome) -> None:
All tests should have the outcome of `success`.
"""
actual = run_tests(cwd, test_ids, pattern, None, "fake-uuid", 1, None)
os.environ["TEST_RUN_PIPE"] = "fake"
actual = run_tests(cwd, test_ids, pattern, None, 1, None)
assert actual
assert all(item in actual for item in ("cwd", "status"))
assert actual["status"] == "success"
Expand All @@ -191,8 +202,10 @@ def test_multiple_ids_run(test_ids, pattern, cwd, expected_outcome) -> None:
assert True


def test_failed_tests():
def test_failed_tests(mock_send_run_data):
"""This test runs on a single file `test_fail` with two tests that fail."""

os.environ["TEST_RUN_PIPE"] = "fake"
test_ids = [
"test_fail_simple.RunFailSimple.test_one_fail",
"test_fail_simple.RunFailSimple.test_two_fail",
Expand All @@ -202,7 +215,6 @@ def test_failed_tests():
test_ids,
"test_fail_simple*",
None,
"fake-uuid",
1,
None,
)
Expand All @@ -226,17 +238,17 @@ def test_failed_tests():
assert True


def test_unknown_id():
def test_unknown_id(mock_send_run_data):
"""This test runs on a unknown test_id, therefore it should return
an error as the outcome as it attempts to find the given test.
"""
os.environ["TEST_RUN_PIPE"] = "fake"
test_ids = ["unknown_id"]
actual = run_tests(
os.fspath(TEST_DATA_PATH),
test_ids,
"test_fail_simple*",
None,
"fake-uuid",
1,
None,
)
Expand All @@ -260,12 +272,13 @@ def test_incorrect_path():
an error as the outcome as it attempts to find the given folder.
"""
test_ids = ["unknown_id"]
os.environ["TEST_RUN_PIPE"] = "fake"

actual = run_tests(
os.fspath(TEST_DATA_PATH / "unknown_folder"),
test_ids,
"test_fail_simple*",
None,
"fake-uuid",
1,
None,
)
Expand Down
1 change: 1 addition & 0 deletions pythonFiles/unittestadapter/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def formatResult(
"subtest": subtest.id() if subtest else None,
}
self.formatted[test_id] = result
test_run_pipe = os.getenv("TEST_RUN_PIPE")
if not test_run_pipe:
print(
"UNITTEST ERROR: TEST_RUN_PIPE is not set at the time of unittest trying to send data. "
Expand Down

0 comments on commit 7c2a66c

Please sign in to comment.