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
This commit is contained in:
Bruno Oliveira 2016-12-01 15:50:08 -02:00
parent 9ed3d76b51
commit bc0f7e6243
3 changed files with 21 additions and 5 deletions

View File

@ -22,6 +22,10 @@
* Fix error message using ``approx`` with complex numbers (`#2082`_). * Fix error message using ``approx`` with complex numbers (`#2082`_).
Thanks `@adler-j`_ for the report and `@nicoddemus`_ for the PR. Thanks `@adler-j`_ for the report and `@nicoddemus`_ for the PR.
* Fixed false-positives warnings from assertion rewrite hook for modules imported more than
once by the ``pytest_plugins`` mechanism.
Thanks `@nicoddemus`_ for the PR.
* Remove internal code meant to support earlier Python 3 versions that produced the side effect * Remove internal code meant to support earlier Python 3 versions that produced the side effect
of leaving ``None`` in ``sys.modules`` when expressions were evaluated by pytest (for example passing a condition of leaving ``None`` in ``sys.modules`` when expressions were evaluated by pytest (for example passing a condition
as a string to ``pytest.mark.skipif``)(`#2103`_). as a string to ``pytest.mark.skipif``)(`#2103`_).

View File

@ -186,16 +186,15 @@ class AssertionRewritingHook(object):
""" """
already_imported = set(names).intersection(set(sys.modules)) already_imported = set(names).intersection(set(sys.modules))
if already_imported: if already_imported:
for name in names: for name in already_imported:
if name not in self._rewritten_names: if name not in self._rewritten_names:
self._warn_already_imported(already_imported) self._warn_already_imported(name)
self._must_rewrite.update(names) self._must_rewrite.update(names)
def _warn_already_imported(self, names): def _warn_already_imported(self, name):
self.config.warn( self.config.warn(
'P1', 'P1',
'Modules are already imported so can not be re-written: %s' % 'Module already imported so can not be re-written: %s' % name)
','.join(names))
def load_module(self, name): def load_module(self, name):
# If there is an existing module object named 'fullname' in # If there is an existing module object named 'fullname' in

View File

@ -682,6 +682,19 @@ def test_rewritten():
hook.mark_rewrite('test_remember_rewritten_modules') hook.mark_rewrite('test_remember_rewritten_modules')
assert warnings == [] assert warnings == []
def test_rewrite_warning_using_pytest_plugins(self, testdir, monkeypatch):
testdir.makepyfile(**{
'conftest.py': "pytest_plugins = ['core', 'gui', 'sci']",
'core.py': "",
'gui.py': "pytest_plugins = ['core', 'sci']",
'sci.py': "pytest_plugins = ['core']",
'test_rewrite_warning_pytest_plugins.py': "def test(): pass",
})
testdir.chdir()
result = testdir.runpytest_subprocess()
result.stdout.fnmatch_lines(['*= 1 passed in *=*'])
assert 'pytest-warning summary' not in result.stdout.str()
class TestAssertionRewriteHookDetails(object): class TestAssertionRewriteHookDetails(object):
def test_loader_is_package_false_for_module(self, testdir): def test_loader_is_package_false_for_module(self, testdir):