[mq]: monkey

--HG--
branch : 1.0.x
This commit is contained in:
holger krekel 2009-08-13 20:10:12 +02:00
parent f0181c35a5
commit 7b906ca763
3 changed files with 30 additions and 6 deletions

View File

@ -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,

View File

@ -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',

View File

@ -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*"])