From 5cbc06a4537461fd5e1f51df38c47733733a9b9f Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 30 May 2019 08:18:28 -0300 Subject: [PATCH] Show test module in the PytestCollectionWarning message Related to #5330 --- changelog/5330.bugfix.rst | 2 ++ src/_pytest/python.py | 6 ++++-- testing/python/collect.py | 19 ++++++++++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 changelog/5330.bugfix.rst diff --git a/changelog/5330.bugfix.rst b/changelog/5330.bugfix.rst new file mode 100644 index 000000000..61c715552 --- /dev/null +++ b/changelog/5330.bugfix.rst @@ -0,0 +1,2 @@ +Show the test module being collected when emitting ``PytestCollectionWarning`` messages for +test classes with ``__init__`` and ``__new__`` methods to make it easier to pin down the problem. diff --git a/src/_pytest/python.py b/src/_pytest/python.py index 035369a59..06d853f78 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -720,7 +720,8 @@ class Class(PyCollector): self.warn( PytestCollectionWarning( "cannot collect test class %r because it has a " - "__init__ constructor" % self.obj.__name__ + "__init__ constructor (from: %s)" + % (self.obj.__name__, self.parent.nodeid) ) ) return [] @@ -728,7 +729,8 @@ class Class(PyCollector): self.warn( PytestCollectionWarning( "cannot collect test class %r because it has a " - "__new__ constructor" % self.obj.__name__ + "__new__ constructor (from: %s)" + % (self.obj.__name__, self.parent.nodeid) ) ) return [] diff --git a/testing/python/collect.py b/testing/python/collect.py index bfd7e672e..501b30a49 100644 --- a/testing/python/collect.py +++ b/testing/python/collect.py @@ -146,7 +146,24 @@ class TestClass(object): result = testdir.runpytest("-rw") result.stdout.fnmatch_lines( [ - "*cannot collect test class 'TestClass1' because it has a __init__ constructor" + "*cannot collect test class 'TestClass1' because it has " + "a __init__ constructor (from: test_class_with_init_warning.py)" + ] + ) + + def test_class_with_new_warning(self, testdir): + testdir.makepyfile( + """ + class TestClass1(object): + def __new__(self): + pass + """ + ) + result = testdir.runpytest("-rw") + result.stdout.fnmatch_lines( + [ + "*cannot collect test class 'TestClass1' because it has " + "a __new__ constructor (from: test_class_with_new_warning.py)" ] )