8000 Use pytest data to extract the parametrised decoration · mjpieters/vscode-python@c7c9607 · GitHub
[go: up one dir, main page]

Skip to content

Commit c7c9607

Browse files
authored
Use pytest data to extract the parametrised decoration
Rather than try and parse out the parametrized portion of the nodeid (delimited by square brackets but possibly containing square brackets), use native [pytest item attributes](https://docs.pytest.org/en/6.2.x/reference.html#function) to separate out the decoration. Better solution for microsoft#17357, fixing microsoft#17676.
1 parent 9bf27f2 commit c7c9607

File tree

1 file changed

+15
-39
lines changed

1 file changed

+15
-39
lines changed

pythonFiles/testing_tools/adapter/pytest/_pytest_item.py

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,21 @@ def parse_item(
158158
# Skip plugin generated tests
159159
if kind is None:
160160
return None, None
161-
(nodeid, parents, fileid, testfunc, parameterized) = _parse_node_id(
162-
item.nodeid, kind
163-
)
161+
162+
if kind == "function" and item.originalname != item.name:
163+
# split out parametrized decorations `node[params]`) before parsing
164+
# and manually attach parametrized portion back in when done.
165+
parameterized = item.name[len(item.originalname) :]
166+
(parentid, parents, fileid, testfunc, _) = _parse_node_id(
167+
item.nodeid[: -len(parameterized)], kind
168+
)
169+
nodeid = f"{parentid}{parameterized}"
170+
parents = [(parentid, item.originalname, kind), *parents]
171+
else:
172+
(nodeid, parents, fileid, testfunc, parameterized) = _parse_node_id(
173+
item.nodeid, kind
174+
)
175+
164176
# Note: testfunc does not necessarily match item.function.__name__.
165177
# This can result from importing a test function from another module.
166178

@@ -434,32 +446,6 @@ def _parse_node_id(
434446
)
435447

436448

437-
def _find_left_bracket(nodeid):
438-
"""Return tuple of part before final bracket open, separator [, and the remainder.
439-
Notes:
440-
Testcase names in case of parametrized tests are wrapped in [<test-case-name>].
441-
Examples:
442-
dirname[sometext]/dirname/testfile.py::testset::testname[testcase]
443-
=> ('dirname[sometext]/dirname/testfile.py::testset::testname', '[', 'testcase]')
444-
dirname/dirname/testfile.py::testset::testname[testcase]
445-
=> ('dirname/dirname/testfile.py::testset::testname', '[', 'testcase]')
446-
dirname/dirname/testfile.py::testset::testname[testcase[x]]
447-
=> ('dirname/dirname/testfile.py::testset::testname', '[', 'testcase[x]]')
448-
"""
449-
if not nodeid.endswith("]"):
450-
return nodeid, "", ""
451-
bracketcount = 0
452-
for index, char in enumerate(nodeid[::-1]):
453-
if char == "]":
454-
bracketcount += 1
455-
elif char == "[":
456-
bracketcount -= 1
457-
if bracketcount == 0:
458-
n = len(nodeid) - 1 - index
459-
return nodeid[:n], nodeid[n], nodeid[n + 1 :]
460-
return nodeid, "", ""
461-
462-
463449
def _iter_nodes(
464450
testid,
465451
kind,
@@ -473,16 +459,6 @@ def _iter_nodes(
473459
if len(nodeid) > len(testid):
474460
testid = "." + _pathsep + testid
475461

476-
if kind == "function" and nodeid.endswith("]"):
477-
funcid, sep, parameterized = _find_left_bracket(nodeid)
478-
if not sep:
479-
raise should_never_reach_here(
480-
nodeid,
481-
# ...
482-
)
483-
yield (nodeid, sep + parameterized, "subtest")
484-
nodeid = funcid
485-
486462
parentid, _, name = nodeid.rpartition("::")
487463
if not parentid:
488464
if kind is None:

0 commit comments

Comments
 (0)
0