diff --git a/python_files/vscode_pytest/__init__.py b/python_files/vscode_pytest/__init__.py index e86b6e155ec3..886bf516b9e2 100644 --- a/python_files/vscode_pytest/__init__.py +++ b/python_files/vscode_pytest/__init__.py @@ -23,6 +23,12 @@ import pytest +PYTEST_INTERNAL_AVAILABLE = False +with contextlib.suppress(ImportError): + from _pytest.python import PyobjMixin + from _pytest._code.code import Code + PYTEST_INTERNAL_AVAILABLE = True + if TYPE_CHECKING: from pluggy import Result from typing_extensions import NotRequired @@ -842,8 +848,20 @@ def create_test_node( Keyword arguments: test_case -- the pytest test case. """ + + 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 + except (AttributeError, OSError, TypeError): + lineno0 = None + + if lineno0 is None: + lineno0 = test_case.location[1] test_case_loc: str = ( - str(test_case.location[1] + 1) if (test_case.location[1] is not None) else "" + 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 { @@ -1261,3 +1279,4 @@ def pytest_plugin_registered(plugin: object, manager: pytest.PytestPluginManager and not manager.hasplugin(plugin_name) ): manager.register(DeferPlugin(), name=plugin_name) +