Consider plugins loaded by PYTEST_PLUGINS for assertion rewrite

Fix #2185
This commit is contained in:
Bruno Oliveira 2017-01-11 17:11:56 -02:00
parent ff309b3584
commit 043aadeaf2
3 changed files with 27 additions and 3 deletions

View File

@ -6,6 +6,10 @@
* pytest no longer recognizes coroutine functions as yield tests (`#2129`_).
Thanks to `@malinoff`_ for the PR.
* Plugins loaded by the ``PYTEST_PLUGINS`` environment variable are now automatically
considered for assertion rewriting (`#2185`_).
Thanks `@nicoddemus`_ for the PR.
* Improve error message when pytest.warns fails (`#2150`_). The type(s) of the
expected warnings and the list of caught warnings is added to the
error message. Thanks `@lesteve`_ for the PR.
@ -23,6 +27,7 @@
.. _#2129: https://github.com/pytest-dev/pytest/issues/2129
.. _#2148: https://github.com/pytest-dev/pytest/issues/2148
.. _#2150: https://github.com/pytest-dev/pytest/issues/2150
.. _#2185: https://github.com/pytest-dev/pytest/issues/2185
3.0.5 (2016-12-05)

View File

@ -399,13 +399,15 @@ class PytestPluginManager(PluginManager):
self.consider_module(conftestmodule)
def consider_env(self):
self._import_plugin_specs(os.environ.get("PYTEST_PLUGINS"))
specs = os.environ.get("PYTEST_PLUGINS")
if specs:
plugins = specs.split(',')
self._import_plugin_specs(plugins)
def consider_module(self, mod):
plugins = getattr(mod, 'pytest_plugins', [])
if isinstance(plugins, str):
plugins = [plugins]
self.rewrite_hook.mark_rewrite(*plugins)
self._import_plugin_specs(plugins)
def _import_plugin_specs(self, spec):
@ -427,6 +429,7 @@ class PytestPluginManager(PluginManager):
importspec = "_pytest." + modname
else:
importspec = modname
self.rewrite_hook.mark_rewrite(modname)
try:
__import__(importspec)
except ImportError as e:

View File

@ -682,7 +682,7 @@ def test_rewritten():
hook.mark_rewrite('test_remember_rewritten_modules')
assert warnings == []
def test_rewrite_warning_using_pytest_plugins(self, testdir, monkeypatch):
def test_rewrite_warning_using_pytest_plugins(self, testdir):
testdir.makepyfile(**{
'conftest.py': "pytest_plugins = ['core', 'gui', 'sci']",
'core.py': "",
@ -695,6 +695,22 @@ def test_rewritten():
result.stdout.fnmatch_lines(['*= 1 passed in *=*'])
assert 'pytest-warning summary' not in result.stdout.str()
def test_rewrite_warning_using_pytest_plugins_env_var(self, testdir, monkeypatch):
monkeypatch.setenv('PYTEST_PLUGINS', 'plugin')
testdir.makepyfile(**{
'plugin.py': "",
'test_rewrite_warning_using_pytest_plugins_env_var.py': """
import plugin
pytest_plugins = ['plugin']
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):