parent
f0181c35a5
commit
7b906ca763
|
@ -16,6 +16,8 @@ Changes between 1.0.0 and 1.0.1
|
||||||
* "Test" prefixed classes are *not* collected by default anymore if they
|
* "Test" prefixed classes are *not* collected by default anymore if they
|
||||||
have an __init__ method
|
have an __init__ method
|
||||||
|
|
||||||
|
* monkeypatch setenv() now accepts a "prepend" parameter
|
||||||
|
|
||||||
* terser reporting of collection error tracebacks
|
* terser reporting of collection error tracebacks
|
||||||
|
|
||||||
* simplified multicall mechanism and plugin architecture,
|
* simplified multicall mechanism and plugin architecture,
|
||||||
|
|
|
@ -4,7 +4,7 @@ safely patch object attributes, dicts and environment variables.
|
||||||
Usage
|
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
|
variables, object attributes or dictionaries. For example, if you want
|
||||||
to set the environment variable ``ENV1`` and patch the
|
to set the environment variable ``ENV1`` and patch the
|
||||||
``os.path.abspath`` function to return a particular value during a test
|
``os.path.abspath`` function to return a particular value during a test
|
||||||
|
@ -22,6 +22,15 @@ old state. After the test function finished execution all
|
||||||
modifications will be reverted. See the `monkeypatch blog post`_
|
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/
|
.. _`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)))
|
self._setitem.insert(0, (dictionary, name, dictionary.get(name, notset)))
|
||||||
dictionary[name] = value
|
dictionary[name] = value
|
||||||
|
|
||||||
def setenv(self, name, value):
|
def setenv(self, name, value, prepend=None):
|
||||||
self.setitem(os.environ, name, str(value))
|
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):
|
def finalize(self):
|
||||||
for obj, name, value in self._setattr:
|
for obj, name, value in self._setattr:
|
||||||
|
@ -111,6 +123,16 @@ def test_setenv():
|
||||||
monkeypatch.finalize()
|
monkeypatch.finalize()
|
||||||
assert 'XYZ123' not in os.environ
|
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):
|
def test_monkeypatch_plugin(testdir):
|
||||||
reprec = testdir.inline_runsource("""
|
reprec = testdir.inline_runsource("""
|
||||||
pytest_plugins = 'pytest_monkeypatch',
|
pytest_plugins = 'pytest_monkeypatch',
|
||||||
|
|
|
@ -4,7 +4,7 @@ from py.__.test.pluginmanager import PluginManager, canonical_importname, collec
|
||||||
class TestBootstrapping:
|
class TestBootstrapping:
|
||||||
def test_consider_env_fails_to_import(self, monkeypatch):
|
def test_consider_env_fails_to_import(self, monkeypatch):
|
||||||
pluginmanager = PluginManager()
|
pluginmanager = PluginManager()
|
||||||
monkeypatch.setitem(os.environ, 'PYTEST_PLUGINS', 'nonexistingmodule')
|
monkeypatch.setenv('PYTEST_PLUGINS', 'nonexisting', prepend=",")
|
||||||
py.test.raises(ImportError, "pluginmanager.consider_env()")
|
py.test.raises(ImportError, "pluginmanager.consider_env()")
|
||||||
|
|
||||||
def test_preparse_args(self):
|
def test_preparse_args(self):
|
||||||
|
@ -50,7 +50,7 @@ class TestBootstrapping:
|
||||||
plugin = py.test.config.pluginmanager.getplugin('x500')
|
plugin = py.test.config.pluginmanager.getplugin('x500')
|
||||||
assert plugin is not None
|
assert plugin is not None
|
||||||
""")
|
""")
|
||||||
monkeypatch.setitem(os.environ, 'PYTEST_PLUGINS', 'pytest_x500')
|
monkeypatch.setenv('PYTEST_PLUGINS', 'pytest_x500', prepend=",")
|
||||||
result = testdir.runpytest(p)
|
result = testdir.runpytest(p)
|
||||||
assert result.ret == 0
|
assert result.ret == 0
|
||||||
extra = result.stdout.fnmatch_lines(["*1 passed in*"])
|
extra = result.stdout.fnmatch_lines(["*1 passed in*"])
|
||||||
|
|
Loading…
Reference in New Issue