Skip to content

Commit be08223

Browse files
committed
Merge branch 'master' into disable_tearDown_and_cleanups_for_post_mortem_debugging
Conflicts: CHANGELOG.rst
2 parents 10b3274 + 67ba8aa commit be08223

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

CHANGELOG.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@
1010
Thanks `@joguSD`_ for the PR.
1111

1212
* Fix ``UnicodeEncodeError`` when string comparison with unicode has failed. (`#1864`_)
13-
Thanks `@AiOO`_ for the PR
13+
Thanks `@AiOO`_ for the PR.
14+
15+
* ``pytest_plugins`` is now handled correctly if defined as a string (as opposed as
16+
a sequence of strings) when modules are considered for assertion rewriting.
17+
Due to this bug, much more modules were being rewritten than necessary
18+
if a test suite uses ``pytest_plugins`` to load internal plugins (`#1888`_).
19+
Thanks `@jaraco`_ for the report and `@nicoddemus`_ for the PR (`#1891`_).
1420

1521
* Do not call tearDown (and cleanups) when running unittest with ``--pdb``
1622
enabled. This allows proper post mortem debugging for all applications
@@ -25,6 +31,8 @@
2531

2632
.. _#1857: https://github.com/pytest-dev/pytest/issues/1857
2733
.. _#1864: https://github.com/pytest-dev/pytest/issues/1864
34+
.. _#1888: https://github.com/pytest-dev/pytest/issues/1888
35+
.. _#1891: https://github.com/pytest-dev/pytest/pull/1891
2836
.. _#1890: https://github.com/pytest-dev/pytest/issues/1890
2937

3038

_pytest/assertion/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ def register_assert_rewrite(*names):
3636
Thus you should make sure to call this before the module is
3737
actually imported, usually in your __init__.py if you are a plugin
3838
using a package.
39+
40+
:raise TypeError: if the given module names are not strings.
3941
"""
42+
for name in names:
43+
if not isinstance(name, str):
44+
msg = 'expected module names as *args, got {0} instead'
45+
raise TypeError(msg.format(repr(names)))
4046
for hook in sys.meta_path:
4147
if isinstance(hook, rewrite.AssertionRewritingHook):
4248
importhook = hook

_pytest/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,8 @@ def consider_env(self):
379379

380380
def consider_module(self, mod):
381381
plugins = getattr(mod, 'pytest_plugins', [])
382+
if isinstance(plugins, str):
383+
plugins = [plugins]
382384
self.rewrite_hook.mark_rewrite(*plugins)
383385
self._import_plugin_specs(plugins)
384386

testing/test_assertion.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,29 @@ def test_foo(check_first):
8484
assert 0
8585
result.stdout.fnmatch_lines([expected])
8686

87+
@pytest.mark.parametrize('mode', ['str', 'list'])
88+
def test_pytest_plugins_rewrite_module_names(self, testdir, mode):
89+
"""Test that pluginmanager correct marks pytest_plugins variables
90+
for assertion rewriting if they are defined as plain strings or
91+
list of strings (#1888).
92+
"""
93+
plugins = '"ham"' if mode == 'str' else '["ham"]'
94+
contents = {
95+
'conftest.py': """
96+
pytest_plugins = {plugins}
97+
""".format(plugins=plugins),
98+
'ham.py': """
99+
import pytest
100+
""",
101+
'test_foo.py': """
102+
def test_foo(pytestconfig):
103+
assert 'ham' in pytestconfig.pluginmanager.rewrite_hook._must_rewrite
104+
""",
105+
}
106+
testdir.makepyfile(**contents)
107+
result = testdir.runpytest_subprocess('--assert=rewrite')
108+
assert result.ret == 0
109+
87110
@pytest.mark.parametrize('mode', ['plain', 'rewrite'])
88111
def test_installed_plugin_rewrite(self, testdir, mode):
89112
# Make sure the hook is installed early enough so that plugins
@@ -196,6 +219,12 @@ def test_other():
196219
'>*assert l.pop() == 3*',
197220
'E*AssertionError'])
198221

222+
def test_register_assert_rewrite_checks_types(self):
223+
with pytest.raises(TypeError):
224+
pytest.register_assert_rewrite(['pytest_tests_internal_non_existing'])
225+
pytest.register_assert_rewrite('pytest_tests_internal_non_existing',
226+
'pytest_tests_internal_non_existing2')
227+
199228

200229
class TestBinReprIntegration:
201230

0 commit comments

Comments
 (0)