| 1 | diff --git a/src/_pytest/_code/source.py b/src/_pytest/_code/source.py |
| 2 | --- a/src/_pytest/_code/source.py |
| 3 | +++ b/src/_pytest/_code/source.py |
| 4 | @@ -149,6 +149,11 @@ def get_statement_startend2(lineno: int, node: ast.AST) -> Tuple[int, Optional[i |
| 5 | values: List[int] = [] |
| 6 | for x in ast.walk(node): |
| 7 | if isinstance(x, (ast.stmt, ast.ExceptHandler)): |
| 8 | + # Before Python 3.8, the lineno of a decorated class or function pointed at the decorator. |
| 9 | + # Since Python 3.8, the lineno points to the class/def, so need to include the decorators. |
| 10 | + if isinstance(x, (ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef)): |
| 11 | + for d in x.decorator_list: |
| 12 | + values.append(d.lineno - 1) |
| 13 | values.append(x.lineno - 1) |
| 14 | for name in ("finalbody", "orelse"): |
| 15 | val: Optional[List[ast.stmt]] = getattr(x, name, None) |