From 6f1eca5e4a938135d2efdc89a00a693fa302bbef Mon Sep 17 00:00:00 2001 From: hpk Date: Tue, 17 Mar 2009 10:18:38 +0100 Subject: [PATCH] [svn r62987] add a "setenv" helper for setting a value in the environment --HG-- branch : trunk --- py/test/plugin/pytest_monkeypatch.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/py/test/plugin/pytest_monkeypatch.py b/py/test/plugin/pytest_monkeypatch.py index 76fed791a..8d8f41382 100644 --- a/py/test/plugin/pytest_monkeypatch.py +++ b/py/test/plugin/pytest_monkeypatch.py @@ -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',