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

Skip to content

Commit 4a011e4

Browse files
committed
Use pytest data to extract the 10000 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 4a011e4

File tree

2 files changed

+29
-39
lines changed

2 files changed

+29
-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 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 = "{}{}".format(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:

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ def test_modifyitems(self):
530530
stub,
531531
nodeid="test_spam.py::SpamTests::test_one",
532532
name="test_one",
533+
originalname="test_one",
533534
location=("test_spam.py", 12, "SpamTests.test_one"),
534535
fspath=adapter_util.PATH_JOIN(testroot, "test_spam.py"),
535536
function=FakeFunc("test_one"),
@@ -538,6 +539,7 @@ def test_modifyitems(self):
538539
stub,
539540
nodeid="test_spam.py::SpamTests::test_other",
540541
name="test_other",
542+
originalname="test_other",
541543
location=("test_spam.py", 19, "SpamTests.test_other"),
542544
fspath=adapter_util.PATH_JOIN(testroot, "test_spam.py"),
543545
function=FakeFunc("test_other"),
@@ -546,6 +548,7 @@ def test_modifyitems(self):
546548
stub,
547549
nodeid="test_spam.py::test_all",
548550
name="test_all",
551+
originalname="test_all",
549552
location=("test_spam.py", 144, "test_all"),
550553
fspath=adapter_util.PATH_JOIN(testroot, "test_spam.py"),
551554
function=FakeFunc("test_all"),
@@ -554,6 +557,7 @@ def test_modifyitems(self):
554557
stub,
555558
nodeid="test_spam.py::test_each[10-10]",
556559
name="test_each[10-10]",
560+
originalname="test_each",
557561
location=("test_spam.py", 273, "test_each[10-10]"),
558562
fspath=adapter_util.PATH_JOIN(testroot, "test_spam.py"),
559563
function=FakeFunc("test_each"),
@@ -562,6 +566,7 @@ def test_modifyitems(self):
562566
stub,
563567
nodeid=relfile2 + "::All::BasicTests::test_first",
564568
name="test_first",
569+
originalname="test_first",
565570
location=(relfile2, 31, "All.BasicTests.test_first"),
566571
fspath=adapter_util.PATH_JOIN(testroot, relfile2),
567572
function=FakeFunc("test_first"),
@@ -570,6 +575,7 @@ def test_modifyitems(self):
570575
stub,
571576
nodeid=relfile2 + "::All::BasicTests::test_each[1+2-3]",
572577
name="test_each[1+2-3]",
578+
originalname="test_each",
573579
location=(relfile2, 62, "All.BasicTests.test_each[1+2-3]"),
574580
fspath=adapter_util.PATH_JOIN(testroot, relfile2),
575581
function=FakeFunc("test_each"),
@@ -781,6 +787,7 @@ def test_finish(self):
781787
stub,
782788
nodeid=relfile + "::SpamTests::test_spam",
783789
name="test_spam",
790+
originalname="test_spam",
784791
location=(relfile, 12, "SpamTests.test_spam"),
785792
fspath=adapter_util.PATH_JOIN(testroot, relfile),
786793
function=FakeFunc("test_spam"),
@@ -992,6 +999,7 @@ def test_nested_brackets(self):
992999
stub,
9931000
nodeid=relfile + "::SpamTests::test_spam[a-[b]-c]",
9941001
name="test_spam[a-[b]-c]",
1002+
originalname="test_spam",
9951003
location=(relfile, 12, "SpamTests.test_spam[a-[b]-c]"),
9961004
fspath=adapter_util.PATH_JOIN(testroot, relfile),
9971005
function=FakeFunc("test_spam"),
@@ -1054,6 +1062,7 @@ def test_nested_suite(self):
10541062
stub,
10551063
nodeid=relfile + "::SpamTests::Ham::Eggs::test_spam",
10561064
name="test_spam",
1065+
originalname="test_spam",
10571066
location=(relfile, 12, "SpamTests.Ham.Eggs.test_spam"),
10581067
fspath=adapter_util.PATH_JOIN(testroot, relfile),
10591068
17AE function=FakeFunc("test_spam"),
@@ -1120,6 +1129,7 @@ def test_windows(self):
11201129
# pytest always uses "/" as the path separator in node IDs:
11211130
nodeid="X/Y/Z/test_Eggs.py::SpamTests::test_spam",
11221131
name="test_spam",
1132+
originalname="test_spam",
11231133
# normal path separator (contrast with nodeid):
11241134
location=(relfile, 12, "SpamTests.test_spam"),
11251135
# path separator matches location:
@@ -1152,6 +1162,7 @@ def test_windows(self):
11521162
stub,
11531163
nodeid=fileid + "::test_spam",
11541164
name="test_spam",
1165+
originalname="test_spam",
11551166
location=(locfile, 12, "test_spam"),
11561167
fspath=fspath,
11571168
function=FakeFunc("test_spam"),
@@ -1412,6 +1423,7 @@ def test_mysterious_parens(self):
14121423
stub,
14131424
nodeid=relfile + "::SpamTests::()::()::test_spam",
14141425
name="test_spam",
1426+
originalname="test_spam",
14151427
location=(relfile, 12, "SpamTests.test_spam"),
14161428
fspath=adapter_util.PATH_JOIN(testroot, relfile),
14171429
function=FakeFunc("test_spam"),
@@ -1472,6 +1484,7 @@ def test_imported_test(self):
14721484
stub,
14731485
nodeid=relfile + "::SpamTests::test_spam",
14741486
name="test_spam",
1487+
originalname="test_spam",
14751488
location=(srcfile, 12, "SpamTests.test_spam"),
14761489
fspath=adapter_util.PATH_JOIN(testroot, relfile),
14771490
function=FakeFunc("test_spam"),
@@ -1480,6 +1493,7 @@ def test_imported_test(self):
14801493
stub,
14811494
nodeid=relfile + "::test_ham",
14821495
name="test_ham",
1496+
originalname="test_ham",
14831497
location=(srcfile, 3, "test_ham"),
14841498
fspath=adapter_util.PATH_JOIN(testroot, relfile),
14851499
function=FakeFunc("test_spam"),

0 commit comments

Comments
 (0)
0