Skip to content

Commit bc0f7e6

Browse files
committed
Fix false-positive assert rewrite warnings when using 'pytest_plugins'
pytest would emit false positive warnings about assertion-rewrite when a module appears multiple times in plugins which depend on other plugins using the 'pytest_plugins' mechanism
1 parent 9ed3d76 commit bc0f7e6

3 files changed

Lines changed: 21 additions & 5 deletions

File tree

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
* Fix error message using ``approx`` with complex numbers (`#2082`_).
2323
Thanks `@adler-j`_ for the report and `@nicoddemus`_ for the PR.
2424

25+
* Fixed false-positives warnings from assertion rewrite hook for modules imported more than
26+
once by the ``pytest_plugins`` mechanism.
27+
Thanks `@nicoddemus`_ for the PR.
28+
2529
* Remove internal code meant to support earlier Python 3 versions that produced the side effect
2630
of leaving ``None`` in ``sys.modules`` when expressions were evaluated by pytest (for example passing a condition
2731
as a string to ``pytest.mark.skipif``)(`#2103`_).

_pytest/assertion/rewrite.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,15 @@ def mark_rewrite(self, *names):
186186
"""
187187
already_imported = set(names).intersection(set(sys.modules))
188188
if already_imported:
189-
for name in names:
189+
for name in already_imported:
190190
if name not in self._rewritten_names:
191-
self._warn_already_imported(already_imported)
191+
self._warn_already_imported(name)
192192
self._must_rewrite.update(names)
193193

194-
def _warn_already_imported(self, names):
194+
def _warn_already_imported(self, name):
195195
self.config.warn(
196196
'P1',
197-
'Modules are already imported so can not be re-written: %s' %
198-
','.join(names))
197+
'Module already imported so can not be re-written: %s' % name)
199198

200199
def load_module(self, name):
201200
# If there is an existing module object named 'fullname' in

testing/test_assertrewrite.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,19 @@ def test_remember_rewritten_modules(self, pytestconfig, testdir, monkeypatch):
682682
hook.mark_rewrite('test_remember_rewritten_modules')
683683
assert warnings == []
684684

685+
def test_rewrite_warning_using_pytest_plugins(self, testdir, monkeypatch):
686+
testdir.makepyfile(**{
687+
'conftest.py': "pytest_plugins = ['core', 'gui', 'sci']",
688+
'core.py': "",
689+
'gui.py': "pytest_plugins = ['core', 'sci']",
690+
'sci.py': "pytest_plugins = ['core']",
691+
'test_rewrite_warning_pytest_plugins.py': "def test(): pass",
692+
})
693+
testdir.chdir()
694+
result = testdir.runpytest_subprocess()
695+
result.stdout.fnmatch_lines(['*= 1 passed in *=*'])
696+
assert 'pytest-warning summary' not in result.stdout.str()
697+
685698

686699
class TestAssertionRewriteHookDetails(object):
687700
def test_loader_is_package_false_for_module(self, testdir):

0 commit comments

Comments
 (0)