[svn r62987] add a "setenv" helper for setting a value in the environment

--HG--
branch : trunk
This commit is contained in:
hpk 2009-03-17 10:18:38 +01:00
parent 881fb3fd05
commit 6f1eca5e4a
1 changed files with 13 additions and 0 deletions

View File

@ -1,3 +1,5 @@
import os
class MonkeypatchPlugin:
""" setattr-monkeypatching with automatical reversal after test. """
def pytest_pyfuncarg_monkeypatch(self, pyfuncitem):
@ -20,6 +22,9 @@ 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 finalize(self):
for obj, name, value in self._setattr:
if value is not notset:
@ -63,6 +68,14 @@ def test_setitem():
assert d['x'] == 1
assert 'y' not in d
def test_setenv():
monkeypatch = MonkeyPatch()
monkeypatch.setenv('XYZ123', 2)
import os
assert os.environ['XYZ123'] == "2"
monkeypatch.finalize()
assert 'XYZ123' not in os.environ
def test_monkeypatch_plugin(testdir):
sorter = testdir.inline_runsource("""
pytest_plugins = 'pytest_monkeypatch',