Skip to content

Commit 7c2a66c

Browse files
committed
fix unittests python tests
1 parent 854a3ad commit 7c2a66c

File tree

3 files changed

+40
-30
lines changed

3 files changed

+40
-30
lines changed

pythonFiles/tests/unittestadapter/test_discovery.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ def test_simple_discovery() -> None:
125125
"id_": start_dir,
126126
}
127127

128-
uuid = "some-uuid"
129-
actual = discover_tests(start_dir, pattern, None, uuid)
128+
actual = discover_tests(start_dir, pattern, None)
130129

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

143-
uuid = "some-uuid"
144-
actual = discover_tests(start_dir, pattern, None, uuid)
142+
actual = discover_tests(start_dir, pattern, None)
145143

146144
assert actual["status"] == "success"
147145
assert "tests" in actual
@@ -206,8 +204,7 @@ def test_error_discovery() -> None:
206204
"id_": start_dir,
207205
}
208206

209-
uuid = "some-uuid"
210-
actual = discover_tests(start_dir, pattern, None, uuid)
207+
actual = discover_tests(start_dir, pattern, None)
211208

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

224-
uuid = "some-uuid"
225-
actual = discover_tests(start_dir, pattern, None, uuid)
221+
actual = discover_tests(start_dir, pattern, None)
226222

227223
assert actual["status"] == "success"
228224
assert "tests" in actual

pythonFiles/tests/unittestadapter/test_execution.py

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
import os
55
import pathlib
66
import sys
7+
from unittest.mock import patch
78

89
import pytest
910

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

13-
from unittestadapter.execution import run_tests
14+
from unittestadapter.execution import run_tests, send_run_data
1415

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

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

3536

36-
def test_single_ids_run() -> None:
37+
@pytest.fixture
38+
def mock_send_run_data():
39+
with patch("unittestadapter.execution.send_run_data") as mock:
40+
yield mock
41+
42+
43+
def test_single_ids_run(mock_send_run_data):
3744
"""This test runs on a single test_id, therefore it should return
3845
a dict with a single key-value pair for the result.
3946
4047
This single test passes so the outcome should be 'success'.
4148
"""
4249
id = "discovery_simple.DiscoverySimple.test_one"
50+
os.environ["TEST_RUN_PIPE"] = "fake"
4351
actual = run_tests(
4452
os.fspath(TEST_DATA_PATH),
4553
[id],
4654
"discovery_simple*",
4755
None,
48-
"fake-uuid",
4956
1,
5057
None,
5158
)
52-
assert actual
53-
assert all(item in actual for item in ("cwd", "status"))
54-
assert actual["status"] == "success"
55-
assert actual["cwd"] == os.fspath(TEST_DATA_PATH)
56-
assert actual["result"] is not None
57-
result = actual["result"]
58-
assert len(result) == 1
59-
assert id in result
60-
id_result = result[id]
59+
60+
# Access the arguments
61+
args, _ = mock_send_run_data.call_args
62+
actual_result = args[0] # first argument is the result
63+
64+
assert actual_result
65+
actual_result = actual["result"]
66+
assert len(actual_result) == 1
67+
assert id in actual_result
68+
id_result = actual_result[id]
6169
assert id_result is not None
6270
assert "outcome" in id_result
6371
assert id_result["outcome"] == "success"
6472

6573

66-
def test_subtest_run() -> None:
74+
def test_subtest_run(mock_send_run_data) -> None:
6775
"""This test runs on a the test_subtest which has a single method, test_even,
6876
that uses unittest subtest.
6977
7078
The actual result of run should return a dict payload with 6 entry for the 6 subtests.
7179
"""
7280
id = "test_subtest.NumbersTest.test_even"
81+
os.environ["TEST_RUN_PIPE"] = "fake"
7382
actual = run_tests(
7483
os.fspath(TEST_DATA_PATH),
7584
[id],
7685
"test_subtest.py",
7786
None,
78-
"fake-uuid",
7987
1,
8088
None,
8189
)
@@ -161,7 +169,9 @@ def test_subtest_run() -> None:
161169
),
162170
],
163171
)
164-
def test_multiple_ids_run(test_ids, pattern, cwd, expected_outcome) -> None:
172+
def test_multiple_ids_run(
173+
mock_send_run_data, test_ids, pattern, cwd, expected_outcome
174+
) -> None:
165175
"""
166176
The following are all successful tests of different formats.
167177
@@ -174,7 +184,8 @@ def test_multiple_ids_run(test_ids, pattern, cwd, expected_outcome) -> None:
174184
175185
All tests should have the outcome of `success`.
176186
"""
177-
actual = run_tests(cwd, test_ids, pattern, None, "fake-uuid", 1, None)
187+
os.environ["TEST_RUN_PIPE"] = "fake"
188+
actual = run_tests(cwd, test_ids, pattern, None, 1, None)
178189
assert actual
179190
assert all(item in actual for item in ("cwd", "status"))
180191
assert actual["status"] == "success"
@@ -191,8 +202,10 @@ def test_multiple_ids_run(test_ids, pattern, cwd, expected_outcome) -> None:
191202
assert True
192203

193204

194-
def test_failed_tests():
205+
def test_failed_tests(mock_send_run_data):
195206
"""This test runs on a single file `test_fail` with two tests that fail."""
207+
208+
os.environ["TEST_RUN_PIPE"] = "fake"
196209
test_ids = [
197210
"test_fail_simple.RunFailSimple.test_one_fail",
198211
"test_fail_simple.RunFailSimple.test_two_fail",
@@ -202,7 +215,6 @@ def test_failed_tests():
202215
test_ids,
203216
"test_fail_simple*",
204217
None,
205-
"fake-uuid",
206218
1,
207219
None,
208220
)
@@ -226,17 +238,17 @@ def test_failed_tests():
226238
assert True
227239

228240

229-
def test_unknown_id():
241+
def test_unknown_id(mock_send_run_data):
230242
"""This test runs on a unknown test_id, therefore it should return
231243
an error as the outcome as it attempts to find the given test.
232244
"""
245+
os.environ["TEST_RUN_PIPE"] = "fake"
233246
test_ids = ["unknown_id"]
234247
actual = run_tests(
235248
os.fspath(TEST_DATA_PATH),
236249
test_ids,
237250
"test_fail_simple*",
238251
None,
239-
"fake-uuid",
240252
1,
241253
None,
242254
)
@@ -260,12 +272,13 @@ def test_incorrect_path():
260272
an error as the outcome as it attempts to find the given folder.
261273
"""
262274
test_ids = ["unknown_id"]
275+
os.environ["TEST_RUN_PIPE"] = "fake"
276+
263277
actual = run_tests(
264278
os.fspath(TEST_DATA_PATH / "unknown_folder"),
265279
test_ids,
266280
"test_fail_simple*",
267281
None,
268-
"fake-uuid",
269282
1,
270283
None,
271284
)

pythonFiles/unittestadapter/execution.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ def formatResult(
133133
"subtest": subtest.id() if subtest else None,
134134
}
135135
self.formatted[test_id] = result
136+
test_run_pipe = os.getenv("TEST_RUN_PIPE")
136137
if not test_run_pipe:
137138
print(
138139
"UNITTEST ERROR: TEST_RUN_PIPE is not set at the time of unittest trying to send data. "

0 commit comments

Comments
 (0)