Skip to content

Commit

Permalink
Merge pull request #2110 from nicoddemus/rewrite-warning-pytest-plugins
Browse files Browse the repository at this point in the history
Fix false-positive assert rewrite warnings when using 'pytest_plugins'
  • Loading branch information
RonnyPfannschmidt authored Dec 1, 2016
2 parents 9ed3d76 + bc0f7e6 commit 64193ad
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
* Fix error message using ``approx`` with complex numbers (`#2082`_).
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
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`_).
Expand Down
9 changes: 4 additions & 5 deletions _pytest/assertion/rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,15 @@ def mark_rewrite(self, *names):
"""
already_imported = set(names).intersection(set(sys.modules))
if already_imported:
for name in names:
for name in already_imported:
if name not in self._rewritten_names:
self._warn_already_imported(already_imported)
self._warn_already_imported(name)
self._must_rewrite.update(names)

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

def load_module(self, name):
# If there is an existing module object named 'fullname' in
Expand Down
13 changes: 13 additions & 0 deletions testing/test_assertrewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,19 @@ def test_remember_rewritten_modules(self, pytestconfig, testdir, monkeypatch):
hook.mark_rewrite('test_remember_rewritten_modules')
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):
def test_loader_is_package_false_for_module(self, testdir):
Expand Down

0 comments on commit 64193ad

Please sign in to comment.