Skip to content

Commit 65e8e52

Browse files
committed
Fix off-by-one error with lineno in mark collection error
1 parent ee96214 commit 65e8e52

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

changelog/5003.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix line offset with mark collection error (off by one).

src/_pytest/_code/source.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ def compile_(source, filename=None, mode="exec", flags=0, dont_inherit=0):
203203

204204
def getfslineno(obj):
205205
""" Return source location (path, lineno) for the given object.
206-
If the source cannot be determined return ("", -1)
206+
If the source cannot be determined return ("", -1).
207+
208+
The line number is 0-based.
207209
"""
208210
from .code import Code
209211

src/_pytest/mark/structures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def get_empty_parameterset_mark(config, argnames, func):
4444
f_name = func.__name__
4545
_, lineno = getfslineno(func)
4646
raise Collector.CollectError(
47-
"Empty parameter set in '%s' at line %d" % (f_name, lineno)
47+
"Empty parameter set in '%s' at line %d" % (f_name, lineno + 1)
4848
)
4949
else:
5050
raise LookupError(requested_mark)

testing/test_mark.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import six
99

1010
import pytest
11+
from _pytest.main import EXIT_INTERRUPTED
1112
from _pytest.mark import EMPTY_PARAMETERSET_OPTION
1213
from _pytest.mark import MarkGenerator as Mark
1314
from _pytest.nodes import Collector
@@ -859,20 +860,33 @@ def test_parameterset_for_fail_at_collect(testdir):
859860

860861
config = testdir.parseconfig()
861862
from _pytest.mark import pytest_configure, get_empty_parameterset_mark
862-
from _pytest.compat import getfslineno
863863

864864
pytest_configure(config)
865865

866-
test_func = all
867-
func_name = test_func.__name__
868-
_, func_lineno = getfslineno(test_func)
869-
expected_errmsg = r"Empty parameter set in '%s' at line %d" % (
870-
func_name,
871-
func_lineno,
872-
)
866+
with pytest.raises(
867+
Collector.CollectError, match=r"Empty parameter set in 'all' at line 0"
868+
):
869+
get_empty_parameterset_mark(config, ["a"], all)
870+
871+
p1 = testdir.makepyfile(
872+
"""
873+
import pytest
873874
874-
with pytest.raises(Collector.CollectError, match=expected_errmsg):
875-
get_empty_parameterset_mark(config, ["a"], test_func)
875+
@pytest.mark.parametrize("empty", [])
876+
def test():
877+
pass
878+
"""
879+
)
880+
result = testdir.runpytest(str(p1))
881+
result.stdout.fnmatch_lines(
882+
[
883+
"collected 0 items / 1 errors",
884+
"* ERROR collecting test_parameterset_for_fail_at_collect.py *",
885+
"Empty parameter set in 'test' at line 3",
886+
"*= 1 error in *",
887+
]
888+
)
889+
assert result.ret == EXIT_INTERRUPTED
876890

877891

878892
def test_parameterset_for_parametrize_bad_markname(testdir):

0 commit comments

Comments
 (0)