Skip to content

Commit c6ad0f8

Browse files
mago0211Eleanor Boyd
andauthored
Accept pytest exit code 1 for some tests cannot be collected (microsoft#20074)
In case you have some tests where not successfully collected (e.g. 10 ok and 1 fail) vscode display only an error in the test explorer. For pytest it is possible to set the flag `--continue-on-collection-errors` but also vscode only display an error and not the other successfully collected tests. Pytest has a specific exit code for this behaviour (1) [https://docs.pytest.org/en/7.1.x/reference/exit-codes.html](https://docs.pytest.org/en/7.1.x/reference/exit-codes.html) Co-authored-by: Eleanor Boyd <eleanorboyd@microsoft.com>
1 parent f62e96c commit c6ad0f8

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

‎pythonFiles/testing_tools/adapter/pytest/_discovery.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ def discover(
3232
if ec == 5:
3333
# No tests were discovered.
3434
pass
35+
elif ec == 1:
36+
# Some tests where collected but with errors.
37+
pass
3538
elif ec != 0:
3639
print(
3740
"equivalent command: {} -m pytest {}".format(

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,31 @@ def test_no_tests_found(self):
403403
self.assertEqual(tests, expected)
404404
self.assertEqual(actual_calls, expected_calls)
405405

406+
def test_found_with_collection_error(self):
407+
stub = util.Stub()
408+
pytest = StubPyTest(stub)
409+
pytest.return_main = 1
410+
plugin = StubPlugin(stub)
411+
expected = []
412+
plugin.discovered = expected
413+
calls = [
414+
("pytest.main", None, {"args": self.DEFAULT_ARGS, "plugins": [plugin]}),
415+
("discovered.parents", None, None),
416+
("discovered.__len__", None, None),
417+
("discovered.__getitem__", (0,), None),
418+
]
419+
420+
parents, tests = _discovery.discover(
421+
[], _pytest_main=pytest.main, _plugin=plugin
422+
)
423+
424+
actual_calls = unique(stub.calls, lambda k: k[0])
425+
expected_calls = unique(calls, lambda k: k[0])
426+
427+
self.assertEqual(parents, [])
428+
self.assertEqual(tests, expected)
429+
self.assertEqual(actual_calls, expected_calls)
430+
406431
def test_stdio_hidden_file(self):
407432
stub = util.Stub()
408433

0 commit comments

Comments
 (0)