Skip to content

Commit c92021f

Browse files
authored
Merge pull request #5003 from blueyed/off
Fix off-by-one error with lineno in mark collection error
2 parents 50a5ceb + 76c70cb commit c92021f

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

changelog/5003.bugfix.rst

+1
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

+3-1
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

+1-1
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

+25-10
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,34 @@ 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,
868+
match=r"Empty parameter set in 'pytest_configure' at line \d\d+",
869+
):
870+
get_empty_parameterset_mark(config, ["a"], pytest_configure)
871+
872+
p1 = testdir.makepyfile(
873+
"""
874+
import pytest
873875
874-
with pytest.raises(Collector.CollectError, match=expected_errmsg):
875-
get_empty_parameterset_mark(config, ["a"], test_func)
876+
@pytest.mark.parametrize("empty", [])
877+
def test():
878+
pass
879+
"""
880+
)
881+
result = testdir.runpytest(str(p1))
882+
result.stdout.fnmatch_lines(
883+
[
884+
"collected 0 items / 1 errors",
885+
"* ERROR collecting test_parameterset_for_fail_at_collect.py *",
886+
"Empty parameter set in 'test' at line 3",
887+
"*= 1 error in *",
888+
]
889+
)
890+
assert result.ret == EXIT_INTERRUPTED
876891

877892

878893
def test_parameterset_for_parametrize_bad_markname(testdir):

0 commit comments

Comments
 (0)