From 7b906ca763e2bed278cc4cb830e2d4b7e43b9fa3 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Thu, 13 Aug 2009 20:10:12 +0200 Subject: [PATCH] [mq]: monkey --HG-- branch : 1.0.x --- CHANGELOG | 2 ++ py/test/plugin/pytest_monkeypatch.py | 30 +++++++++++++++++++++++---- py/test/testing/test_pluginmanager.py | 4 ++-- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index ed1b2746e..fa06cb3a8 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -16,6 +16,8 @@ Changes between 1.0.0 and 1.0.1 * "Test" prefixed classes are *not* collected by default anymore if they have an __init__ method +* monkeypatch setenv() now accepts a "prepend" parameter + * terser reporting of collection error tracebacks * simplified multicall mechanism and plugin architecture, diff --git a/py/test/plugin/pytest_monkeypatch.py b/py/test/plugin/pytest_monkeypatch.py index 56fb61b0e..80984b6f0 100644 --- a/py/test/plugin/pytest_monkeypatch.py +++ b/py/test/plugin/pytest_monkeypatch.py @@ -4,7 +4,7 @@ safely patch object attributes, dicts and environment variables. Usage ---------------- -Use the `monkeypatch funcarg`_ to safely patch the environment +Use the `monkeypatch funcarg`_ to safely patch environment variables, object attributes or dictionaries. For example, if you want to set the environment variable ``ENV1`` and patch the ``os.path.abspath`` function to return a particular value during a test @@ -20,7 +20,16 @@ function execution you can write it down like this: The function argument will do the modifications and memorize the old state. After the test function finished execution all modifications will be reverted. See the `monkeypatch blog post`_ -for an extensive discussion. +for an extensive discussion. + +To add to a possibly existing environment parameter you +can use this example: + +.. sourcecode:: python + + def test_mypath_finding(monkeypatch): + monkeypatch.setenv('PATH', 'x/y', prepend=":") + # x/y will be at the beginning of $PATH .. _`monkeypatch blog post`: http://tetamap.wordpress.com/2009/03/03/monkeypatching-in-unit-tests-done-right/ """ @@ -57,8 +66,11 @@ class MonkeyPatch: self._setitem.insert(0, (dictionary, name, dictionary.get(name, notset))) dictionary[name] = value - def setenv(self, name, value): - self.setitem(os.environ, name, str(value)) + def setenv(self, name, value, prepend=None): + value = str(value) + if prepend and name in os.environ: + value = value + prepend + os.environ[name] + self.setitem(os.environ, name, value) def finalize(self): for obj, name, value in self._setattr: @@ -111,6 +123,16 @@ def test_setenv(): monkeypatch.finalize() assert 'XYZ123' not in os.environ +def test_setenv_prepend(): + import os + monkeypatch = MonkeyPatch() + monkeypatch.setenv('XYZ123', 2, prepend="-") + assert os.environ['XYZ123'] == "2" + monkeypatch.setenv('XYZ123', 3, prepend="-") + assert os.environ['XYZ123'] == "3-2" + monkeypatch.finalize() + assert 'XYZ123' not in os.environ + def test_monkeypatch_plugin(testdir): reprec = testdir.inline_runsource(""" pytest_plugins = 'pytest_monkeypatch', diff --git a/py/test/testing/test_pluginmanager.py b/py/test/testing/test_pluginmanager.py index 3e96e8683..fa19fa4f0 100644 --- a/py/test/testing/test_pluginmanager.py +++ b/py/test/testing/test_pluginmanager.py @@ -4,7 +4,7 @@ from py.__.test.pluginmanager import PluginManager, canonical_importname, collec class TestBootstrapping: def test_consider_env_fails_to_import(self, monkeypatch): pluginmanager = PluginManager() - monkeypatch.setitem(os.environ, 'PYTEST_PLUGINS', 'nonexistingmodule') + monkeypatch.setenv('PYTEST_PLUGINS', 'nonexisting', prepend=",") py.test.raises(ImportError, "pluginmanager.consider_env()") def test_preparse_args(self): @@ -50,7 +50,7 @@ class TestBootstrapping: plugin = py.test.config.pluginmanager.getplugin('x500') assert plugin is not None """) - monkeypatch.setitem(os.environ, 'PYTEST_PLUGINS', 'pytest_x500') + monkeypatch.setenv('PYTEST_PLUGINS', 'pytest_x500', prepend=",") result = testdir.runpytest(p) assert result.ret == 0 extra = result.stdout.fnmatch_lines(["*1 passed in*"])