Skip to content

Commit 7681552

Browse files
committed
Ensure pytest stub calls check for unique calls rather than adjusting the calls by version. (#19296)
1 parent f6a449f commit 7681552

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

pythonFiles/tests/testing_tools/adapter/pytest/test_discovery.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,17 @@
2222

2323
from .... import util
2424

25-
# In Python 3.8 __len__ is called twice, which impacts some of the test assertions we do below.
26-
PYTHON_38_OR_LATER = sys.version_info[0] >= 3 and sys.version_info[1] >= 8
25+
26+
def unique(collection, key):
27+
result = []
28+
keys = []
29+
for item in collection:
30+
k = key(item)
31+
if k in keys:
32+
continue
33+
result.append(item)
34+
keys.append(k)
35+
return result
2736

2837

2938
class StubPyTest(util.StubProxy):
@@ -341,17 +350,16 @@ def test_basic(self):
341350
("discovered.__getitem__", (0,), None),
342351
]
343352

344-
# In Python 3.8 __len__ is called twice.
345-
if PYTHON_38_OR_LATER:
346-
calls.insert(3, ("discovered.__len__", None, None))
347-
348353
parents, tests = _discovery.discover(
349354
[], _pytest_main=stubpytest.main, _plugin=plugin
350355
)
351356

357+
actual_calls = unique(stub.calls, lambda k: k[0])
358+
expected_calls = unique(calls, lambda k: k[0])
359+
352360
self.assertEqual(parents, [])
353361
self.assertEqual(tests, expected)
354-
self.assertEqual(stub.calls, calls)
362+
self.assertEqual(actual_calls, expected_calls)
355363

356364
def test_failure(self):
357365
stub = util.Stub()
@@ -384,17 +392,16 @@ def test_no_tests_found(self):
384392
("discovered.__getitem__", (0,), None),
385393
]
386394

387-
# In Python 3.8 __len__ is called twice.
388-
if PYTHON_38_OR_LATER:
389-
calls.insert(3, ("discovered.__len__", None, None))
390-
391395
parents, tests = _discovery.discover(
392396
[], _pytest_main=pytest.main, _plugin=plugin
393397
)
394398

399+
actual_calls = unique(stub.calls, lambda k: k[0])
400+
expected_calls = unique(calls, lambda k: k[0])
401+
395402
self.assertEqual(parents, [])
396403
self.assertEqual(tests, expected)
397-
self.assertEqual(stub.calls, calls)
404+
self.assertEqual(actual_calls, expected_calls)
398405

399406
def test_stdio_hidden_file(self):
400407
stub = util.Stub()
@@ -409,10 +416,6 @@ def test_stdio_hidden_file(self):
409416
]
410417
pytest_stdout = "spamspamspamspamspamspamspammityspam"
411418

412-
# In Python 3.8 __len__ is called twice.
413-
if PYTHON_38_OR_LATER:
414-
calls.insert(3, ("discovered.__len__", None, None))
415-
416419
# to simulate stdio behavior in methods like os.dup,
417420
# use actual files (rather than StringIO)
418421
with tempfile.TemporaryFile("r+") as mock:
@@ -430,8 +433,11 @@ def test_stdio_hidden_file(self):
430433
mock.seek(0)
431434
captured = mock.read()
432435

436+
actual_calls = unique(stub.calls, lambda k: k[0])
437+
expected_calls = unique(calls, lambda k: k[0])
438+
433439
self.assertEqual(captured, "")
434-
self.assertEqual(stub.calls, calls)
440+
self.assertEqual(actual_calls, expected_calls)
435441

436442
def test_stdio_hidden_fd(self):
437443
# simulate cases where stdout comes from the lower layer than sys.stdout
@@ -467,10 +473,6 @@ def test_stdio_not_hidden_file(self):
467473
]
468474
pytest_stdout = "spamspamspamspamspamspamspammityspam"
469475

470-
# In Python 3.8 __len__ is called twice.
471-
if PYTHON_38_OR_LATER:
472-
calls.insert(3, ("discovered.__len__", None, None))
473-
474476
buf = StringIO()
475477

476478
sys.stdout = buf
@@ -485,8 +487,11 @@ def test_stdio_not_hidden_file(self):
485487
sys.stdout = sys.__stdout__
486488
captured = buf.getvalue()
487489

490+
actual_calls = unique(stub.calls, lambda k: k[0])
491+
expected_calls = unique(calls, lambda k: k[0])
492+
488493
self.assertEqual(captured, pytest_stdout)
489-
self.assertEqual(stub.calls, calls)
494+
self.assertEqual(actual_calls, expected_calls)
490495

491496
def test_stdio_not_hidden_fd(self):
492497
# simulate cases where stdout comes from the lower layer than sys.stdout

0 commit comments

Comments
 (0)