I want to be able to set environment variables in my Django app for tests to be able to run. For instance, my views rely on several API keys.
There are ways to override settings during testing, but I don't want them defined in
os.environ['TEST'] = '123' # doesn't propogate to app
foreman start -e .env web
As @schillingt noted in the comments, EnvironmentVarGuard was the correct way.
from test.test_support import EnvironmentVarGuard from django.test import TestCase class MyTestCase(TestCase): def setUp(self): self.env = EnvironmentVarGuard() self.env.set('VAR', 'value') def test_something(self): with self.env: # ... perform tests here ... # pass
This correctly sets environment variables for the duration of the context object