4
4
import os
5
5
import pathlib
6
6
import sys
7
+ from unittest .mock import patch
7
8
8
9
import pytest
9
10
10
11
script_dir = pathlib .Path (__file__ ).parent .parent
11
12
sys .path .insert (0 , os .fspath (script_dir / "lib" / "python" ))
12
13
13
- from unittestadapter .execution import run_tests
14
+ from unittestadapter .execution import run_tests , send_run_data
14
15
15
16
TEST_DATA_PATH = pathlib .Path (__file__ ).parent / ".data"
16
17
@@ -22,7 +23,7 @@ def test_no_ids_run() -> None:
22
23
start_dir : str = os .fspath (TEST_DATA_PATH )
23
24
testids = []
24
25
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 )
26
27
assert actual
27
28
assert all (item in actual for item in ("cwd" , "status" ))
28
29
assert actual ["status" ] == "success"
@@ -33,49 +34,56 @@ def test_no_ids_run() -> None:
33
34
raise AssertionError ("actual['result'] is None" )
34
35
35
36
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 ):
37
44
"""This test runs on a single test_id, therefore it should return
38
45
a dict with a single key-value pair for the result.
39
46
40
47
This single test passes so the outcome should be 'success'.
41
48
"""
42
49
id = "discovery_simple.DiscoverySimple.test_one"
50
+ os .environ ["TEST_RUN_PIPE" ] = "fake"
43
51
actual = run_tests (
44
52
os .fspath (TEST_DATA_PATH ),
45
53
[id ],
46
54
"discovery_simple*" ,
47
55
None ,
48
- "fake-uuid" ,
49
56
1 ,
50
57
None ,
51
58
)
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 ]
61
69
assert id_result is not None
62
70
assert "outcome" in id_result
63
71
assert id_result ["outcome" ] == "success"
64
72
65
73
66
- def test_subtest_run () -> None :
74
+ def test_subtest_run (mock_send_run_data ) -> None :
67
75
"""This test runs on a the test_subtest which has a single method, test_even,
68
76
that uses unittest subtest.
69
77
70
78
The actual result of run should return a dict payload with 6 entry for the 6 subtests.
71
79
"""
72
80
id = "test_subtest.NumbersTest.test_even"
81
+ os .environ ["TEST_RUN_PIPE" ] = "fake"
73
82
actual = run_tests (
74
83
os .fspath (TEST_DATA_PATH ),
75
84
[id ],
76
85
"test_subtest.py" ,
77
86
None ,
78
- "fake-uuid" ,
79
87
1 ,
80
88
None ,
81
89
)
@@ -161,7 +169,9 @@ def test_subtest_run() -> None:
161
169
),
162
170
],
163
171
)
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 :
165
175
"""
166
176
The following are all successful tests of different formats.
167
177
@@ -174,7 +184,8 @@ def test_multiple_ids_run(test_ids, pattern, cwd, expected_outcome) -> None:
174
184
175
185
All tests should have the outcome of `success`.
176
186
"""
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 )
178
189
assert actual
179
190
assert all (item in actual for item in ("cwd" , "status" ))
180
191
assert actual ["status" ] == "success"
@@ -191,8 +202,10 @@ def test_multiple_ids_run(test_ids, pattern, cwd, expected_outcome) -> None:
191
202
assert True
192
203
193
204
194
- def test_failed_tests ():
205
+ def test_failed_tests (mock_send_run_data ):
195
206
"""This test runs on a single file `test_fail` with two tests that fail."""
207
+
208
+ os .environ ["TEST_RUN_PIPE" ] = "fake"
196
209
test_ids = [
197
210
"test_fail_simple.RunFailSimple.test_one_fail" ,
198
211
"test_fail_simple.RunFailSimple.test_two_fail" ,
@@ -202,7 +215,6 @@ def test_failed_tests():
202
215
test_ids ,
203
216
"test_fail_simple*" ,
204
217
None ,
205
- "fake-uuid" ,
206
218
1 ,
207
219
None ,
208
220
)
@@ -226,17 +238,17 @@ def test_failed_tests():
226
238
assert True
227
239
228
240
229
- def test_unknown_id ():
241
+ def test_unknown_id (mock_send_run_data ):
230
242
"""This test runs on a unknown test_id, therefore it should return
231
243
an error as the outcome as it attempts to find the given test.
232
244
"""
245
+ os .environ ["TEST_RUN_PIPE" ] = "fake"
233
246
test_ids = ["unknown_id" ]
234
247
actual = run_tests (
235
248
os .fspath (TEST_DATA_PATH ),
236
249
test_ids ,
237
250
"test_fail_simple*" ,
238
251
None ,
239
- "fake-uuid" ,
240
252
1 ,
241
253
None ,
242
254
)
@@ -260,12 +272,13 @@ def test_incorrect_path():
260
272
an error as the outcome as it attempts to find the given folder.
261
273
"""
262
274
test_ids = ["unknown_id" ]
275
+ os .environ ["TEST_RUN_PIPE" ] = "fake"
276
+
263
277
actual = run_tests (
264
278
os .fspath (TEST_DATA_PATH / "unknown_folder" ),
265
279
test_ids ,
266
280
"test_fail_simple*" ,
267
281
None ,
268
- "fake-uuid" ,
269
282
1 ,
270
283
None ,
271
284
)
0 commit comments