[svn r62987] add a "setenv" helper for setting a value in the environment
--HG-- branch : trunk
This commit is contained in:
parent
881fb3fd05
commit
6f1eca5e4a
|
@ -1,3 +1,5 @@
|
||||||
|
import os
|
||||||
|
|
||||||
class MonkeypatchPlugin:
|
class MonkeypatchPlugin:
|
||||||
""" setattr-monkeypatching with automatical reversal after test. """
|
""" setattr-monkeypatching with automatical reversal after test. """
|
||||||
def pytest_pyfuncarg_monkeypatch(self, pyfuncitem):
|
def pytest_pyfuncarg_monkeypatch(self, pyfuncitem):
|
||||||
|
@ -20,6 +22,9 @@ 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):
|
||||||
|
self.setitem(os.environ, name, str(value))
|
||||||
|
|
||||||
def finalize(self):
|
def finalize(self):
|
||||||
for obj, name, value in self._setattr:
|
for obj, name, value in self._setattr:
|
||||||
if value is not notset:
|
if value is not notset:
|
||||||
|
@ -63,6 +68,14 @@ def test_setitem():
|
||||||
assert d['x'] == 1
|
assert d['x'] == 1
|
||||||
assert 'y' not in d
|
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):
|
def test_monkeypatch_plugin(testdir):
|
||||||
sorter = testdir.inline_runsource("""
|
sorter = testdir.inline_runsource("""
|
||||||
pytest_plugins = 'pytest_monkeypatch',
|
pytest_plugins = 'pytest_monkeypatch',
|
||||||
|
|
Loading…
Reference in New Issue