Skip to content

Commit 7a829cb

Browse files
authored
Document the location tuple (pytest-dev#10700)
1 parent 5e1c3d2 commit 7a829cb

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

src/_pytest/hookspec.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,9 @@ def pytest_runtest_logstart(
505505
See :hook:`pytest_runtest_protocol` for a description of the runtest protocol.
506506
507507
:param nodeid: Full node ID of the item.
508-
:param location: A tuple of ``(filename, lineno, testname)``.
508+
:param location: A tuple of ``(filename, lineno, testname)``
509+
where ``filename`` is a file path relative to ``config.rootpath``
510+
and ``lineno`` is 0-based.
509511
"""
510512

511513

@@ -517,7 +519,9 @@ def pytest_runtest_logfinish(
517519
See :hook:`pytest_runtest_protocol` for a description of the runtest protocol.
518520
519521
:param nodeid: Full node ID of the item.
520-
:param location: A tuple of ``(filename, lineno, testname)``.
522+
:param location: A tuple of ``(filename, lineno, testname)``
523+
where ``filename`` is a file path relative to ``config.rootpath``
524+
and ``lineno`` is 0-based.
521525
"""
522526

523527

src/_pytest/nodes.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ def get_fslocation_from_item(node: "Node") -> Tuple[Union[str, Path], Optional[i
511511
* "obj": a Python object that the node wraps.
512512
* "fspath": just a path
513513
514-
:rtype: A tuple of (str|Path, int) with filename and line number.
514+
:rtype: A tuple of (str|Path, int) with filename and 0-based line number.
515515
"""
516516
# See Item.location.
517517
location: Optional[Tuple[str, Optional[int], str]] = getattr(node, "location", None)
@@ -755,7 +755,7 @@ def reportinfo(self) -> Tuple[Union["os.PathLike[str]", str], Optional[int], str
755755
Returns a tuple with three elements:
756756
757757
- The path of the test (default ``self.path``)
758-
- The line number of the test (default ``None``)
758+
- The 0-based line number of the test (default ``None``)
759759
- A name of the test to be shown (default ``""``)
760760
761761
.. seealso:: :ref:`non-python tests`
@@ -764,6 +764,11 @@ def reportinfo(self) -> Tuple[Union["os.PathLike[str]", str], Optional[int], str
764764

765765
@cached_property
766766
def location(self) -> Tuple[str, Optional[int], str]:
767+
"""
768+
Returns a tuple of ``(relfspath, lineno, testname)`` for this item
769+
where ``relfspath`` is file path relative to ``config.rootpath``
770+
and lineno is a 0-based line number.
771+
"""
767772
location = self.reportinfo()
768773
path = absolutepath(os.fspath(location[0]))
769774
relfspath = self.session._node_location_to_relpath(path)

src/_pytest/reports.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ def __init__(
273273
#: A (filesystempath, lineno, domaininfo) tuple indicating the
274274
#: actual location of a test item - it might be different from the
275275
#: collected one e.g. if a method is inherited from a different module.
276+
#: The filesystempath may be relative to ``config.rootdir``.
277+
#: The line number is 0-based.
276278
self.location: Tuple[str, Optional[int], str] = location
277279

278280
#: A name -> value dictionary containing all keywords and
@@ -417,7 +419,9 @@ def __init__(
417419
self.__dict__.update(extra)
418420

419421
@property
420-
def location(self):
422+
def location( # type:ignore[override]
423+
self,
424+
) -> Optional[Tuple[str, Optional[int], str]]:
421425
return (self.fspath, None, self.fspath)
422426

423427
def __repr__(self) -> str:

testing/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ class TestClass(object):
473473
assert not rep.skipped
474474
assert rep.passed
475475
locinfo = rep.location
476+
assert locinfo is not None
476477
assert locinfo[0] == col.path.name
477478
assert not locinfo[1]
478479
assert locinfo[2] == col.path.name

0 commit comments

Comments
 (0)