Skip to content

Commit dcc0a64

Browse files
committed
Consider plugins loaded by PYTEST_PLUGINS for assertion rewrite
Fix pytest-dev#2185
1 parent ff309b3 commit dcc0a64

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

CHANGELOG.rst

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
* pytest no longer recognizes coroutine functions as yield tests (`#2129`_).
77
Thanks to `@malinoff`_ for the PR.
88

9+
* Plugins configured by the ``PYTEST_PLUGINS`` environment variables are now automatically
10+
considered for assertion rewriting (`#2185`_).
11+
Thanks `@nicoddemus`_ for the PR.
12+
913
* Improve error message when pytest.warns fails (`#2150`_). The type(s) of the
1014
expected warnings and the list of caught warnings is added to the
1115
error message. Thanks `@lesteve`_ for the PR.
@@ -23,6 +27,7 @@
2327
.. _#2129: https://github.com/pytest-dev/pytest/issues/2129
2428
.. _#2148: https://github.com/pytest-dev/pytest/issues/2148
2529
.. _#2150: https://github.com/pytest-dev/pytest/issues/2150
30+
.. _#2185: https://github.com/pytest-dev/pytest/issues/2185
2631

2732

2833
3.0.5 (2016-12-05)

_pytest/config.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -399,13 +399,15 @@ def consider_conftest(self, conftestmodule):
399399
self.consider_module(conftestmodule)
400400

401401
def consider_env(self):
402-
self._import_plugin_specs(os.environ.get("PYTEST_PLUGINS"))
402+
specs = os.environ.get("PYTEST_PLUGINS")
403+
if specs:
404+
plugins = [x.strip() for x in specs.split(',')]
405+
self._import_plugin_specs(plugins)
403406

404407
def consider_module(self, mod):
405408
plugins = getattr(mod, 'pytest_plugins', [])
406409
if isinstance(plugins, str):
407410
plugins = [plugins]
408-
self.rewrite_hook.mark_rewrite(*plugins)
409411
self._import_plugin_specs(plugins)
410412

411413
def _import_plugin_specs(self, spec):
@@ -427,6 +429,7 @@ def import_plugin(self, modname):
427429
importspec = "_pytest." + modname
428430
else:
429431
importspec = modname
432+
self.rewrite_hook.mark_rewrite(modname)
430433
try:
431434
__import__(importspec)
432435
except ImportError as e:

testing/test_assertrewrite.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ 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):
685+
def test_rewrite_warning_using_pytest_plugins(self, testdir):
686686
testdir.makepyfile(**{
687687
'conftest.py': "pytest_plugins = ['core', 'gui', 'sci']",
688688
'core.py': "",
@@ -695,6 +695,22 @@ def test_rewrite_warning_using_pytest_plugins(self, testdir, monkeypatch):
695695
result.stdout.fnmatch_lines(['*= 1 passed in *=*'])
696696
assert 'pytest-warning summary' not in result.stdout.str()
697697

698+
def test_rewrite_warning_using_pytest_plugins_env_var(self, testdir, monkeypatch):
699+
monkeypatch.setenv('PYTEST_PLUGINS', 'plugin')
700+
testdir.makepyfile(**{
701+
'plugin.py': "",
702+
'test_rewrite_warning_using_pytest_plugins_env_var.py': """
703+
import plugin
704+
pytest_plugins = ['plugin']
705+
def test():
706+
pass
707+
""",
708+
})
709+
testdir.chdir()
710+
result = testdir.runpytest_subprocess()
711+
result.stdout.fnmatch_lines(['*= 1 passed in *=*'])
712+
assert 'pytest-warning summary' not in result.stdout.str()
713+
698714

699715
class TestAssertionRewriteHookDetails(object):
700716
def test_loader_is_package_false_for_module(self, testdir):

0 commit comments

Comments
 (0)