Skip to content

Commit 80c2e0d

Browse files
committed
Quick cleanup to make each test name/node id unique
1 parent 8fdfe93 commit 80c2e0d

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/pytest_markdown_docs/plugin.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,15 @@ def extract_options_from_mdx_comment(comment: str) -> typing.Set[str]:
220220

221221

222222
def find_object_tests_recursive(
223-
module_name: str, object_name: str, object: typing.Any
224-
) -> typing.Generator[typing.Tuple[str, typing.List[str], int], None, None]:
223+
module_name: str, object: typing.Any
224+
) -> typing.Generator[
225+
typing.Tuple[typing.Any, typing.Tuple[str, typing.List[str], int]], None, None
226+
]:
225227
docstr = inspect.getdoc(object)
226228

227229
if docstr:
228-
yield from extract_code_blocks(docstr)
230+
for code_block in extract_code_blocks(docstr):
231+
yield object, code_block
229232

230233
for member_name, member in inspect.getmembers(object):
231234
if member_name.startswith("_"):
@@ -236,7 +239,7 @@ def find_object_tests_recursive(
236239
or inspect.isfunction(member)
237240
or inspect.ismethod(member)
238241
) and member.__module__ == module_name:
239-
yield from find_object_tests_recursive(module_name, member_name, member)
242+
yield from find_object_tests_recursive(module_name, member)
240243

241244

242245
class MarkdownDocstringCodeModule(pytest.Module):
@@ -250,12 +253,17 @@ def collect(self):
250253
# but unsupported before 8.1...
251254
module = import_path(self.path, root=self.config.rootpath)
252255

253-
for i, (test_code, fixture_names, start_line) in enumerate(
254-
find_object_tests_recursive(module.__name__, module.__name__, module)
256+
for i, (obj, (test_code, fixture_names, start_line)) in enumerate(
257+
find_object_tests_recursive(module.__name__, module)
255258
):
259+
obj_name = (
260+
getattr(obj, "__qualname__", None)
261+
or getattr(obj, "__name__", None)
262+
or "<Unnamed obj>"
263+
)
256264
yield MarkdownInlinePythonItem.from_parent(
257265
self,
258-
name=f"{self.path}#{i+1}",
266+
name=f"{obj_name}[CodeBlock#{i+1}][rel.line:{start_line}]",
259267
code=test_code,
260268
fixture_names=fixture_names,
261269
start_line=start_line,
@@ -267,12 +275,14 @@ class MarkdownTextFile(pytest.File):
267275
def collect(self):
268276
markdown_content = self.path.read_text("utf8")
269277

270-
for code_block, fixture_names, start_line in extract_code_blocks(
271-
markdown_content, markdown_type=self.path.suffix.replace(".", "")
278+
for i, (code_block, fixture_names, start_line) in enumerate(
279+
extract_code_blocks(
280+
markdown_content, markdown_type=self.path.suffix.replace(".", "")
281+
)
272282
):
273283
yield MarkdownInlinePythonItem.from_parent(
274284
self,
275-
name=str(self.path),
285+
name=f"[CodeBlock#{i+1}][line:{start_line}]",
276286
code=code_block,
277287
fixture_names=fixture_names,
278288
start_line=start_line,

0 commit comments

Comments
 (0)