Skip to content

Commit 5cbc06a

Browse files
committed
Show test module in the PytestCollectionWarning message
Related to #5330
1 parent 72fc439 commit 5cbc06a

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

changelog/5330.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Show the test module being collected when emitting ``PytestCollectionWarning`` messages for
2+
test classes with ``__init__`` and ``__new__`` methods to make it easier to pin down the problem.

src/_pytest/python.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,15 +720,17 @@ def collect(self):
720720
self.warn(
721721
PytestCollectionWarning(
722722
"cannot collect test class %r because it has a "
723-
"__init__ constructor" % self.obj.__name__
723+
"__init__ constructor (from: %s)"
724+
% (self.obj.__name__, self.parent.nodeid)
724725
)
725726
)
726727
return []
727728
elif hasnew(self.obj):
728729
self.warn(
729730
PytestCollectionWarning(
730731
"cannot collect test class %r because it has a "
731-
"__new__ constructor" % self.obj.__name__
732+
"__new__ constructor (from: %s)"
733+
% (self.obj.__name__, self.parent.nodeid)
732734
)
733735
)
734736
return []

testing/python/collect.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,24 @@ def __init__(self):
146146
result = testdir.runpytest("-rw")
147147
result.stdout.fnmatch_lines(
148148
[
149-
"*cannot collect test class 'TestClass1' because it has a __init__ constructor"
149+
"*cannot collect test class 'TestClass1' because it has "
150+
"a __init__ constructor (from: test_class_with_init_warning.py)"
151+
]
152+
)
153+
154+
def test_class_with_new_warning(self, testdir):
155+
testdir.makepyfile(
156+
"""
157+
class TestClass1(object):
158+
def __new__(self):
159+
pass
160+
"""
161+
)
162+
result = testdir.runpytest("-rw")
163+
result.stdout.fnmatch_lines(
164+
[
165+
"*cannot collect test class 'TestClass1' because it has "
166+
"a __new__ constructor (from: test_class_with_new_warning.py)"
150167
]
151168
)
152169

0 commit comments

Comments
 (0)