Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions python_files/vscode_pytest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@

import pytest

PYTEST_INTERNAL_AVAILABLE = False
with contextlib.suppress(ImportError):
from _pytest._code.code import Code

PYTEST_INTERNAL_AVAILABLE = True

if TYPE_CHECKING:
from pluggy import Result
from typing_extensions import NotRequired
Expand Down Expand Up @@ -842,9 +848,18 @@ def create_test_node(
Keyword arguments:
test_case -- the pytest test case.
"""
test_case_loc: str = (
str(test_case.location[1] + 1) if (test_case.location[1] is not None) else ""
)
lineno0: int | None = None
if PYTEST_INTERNAL_AVAILABLE:
obj = getattr(test_case, "obj", None)
if obj is not None:
try:
lineno0 = Code.from_function(obj).firstlineno

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytest unwraps decorated functions before computing their source line, but this direct call inspects the wrapper. I reproduced this with functools.wraps: Code.from_function(obj).firstlineno was 3 while pytest's getfslineno(obj)[1] was 7, and create_test_node() emitted line 5 instead of the fallback/location line 8. Please unwrap using pytest-compatible behavior before Code.from_function, and add an exact-line regression for a decorated test.

except (AttributeError, OSError, TypeError):
lineno0 = None

if lineno0 is None:
lineno0 = test_case.location[1]
test_case_loc: str = str(lineno0 + 1) if (lineno0 is not None) else ""
absolute_test_id = get_absolute_test_id(test_case.nodeid, get_node_path(test_case))
return {
"name": test_case.name,
Expand Down
Loading