class MyTestCase(TestCase):

    def setUp(self):
        self.env = EnvironmentVarGuard()
self.env.set('VAR', 'value')

def test_something(self):
    with self.env:
        pass
django setting environment variables in unittest tests