I have a test suite that gets executed as a part of a larger build framework, written in Python. Some of the tests require parameters, which I want to pass using environment variables.
Apparently the nosetests runner has an env
parameter, which does what I want, according to the documentation. It seems, however, that it does not work as thought it should?
Here's a minimal test script that exemplifies the problem:
#!/usr/bin/env python
# pip install nose
import os, nose, unittest
class Test(unittest.TestCase):
def test_env(self):
self.assertEquals(os.environ.get('HELLO'), 'WORLD')
if __name__ == '__main__':
nose.run(env={'HELLO': 'WORLD'})
The assertion fails, because the env
parameter does not get passed to the test. Does anyone know why?
NB: I worked around the problem by launching the console nosetests
tool:
#!/usr/bin/env python
import sys, os, nose, unittest, subprocess
class Test(unittest.TestCase):
def test_env(self):
self.assertEquals(os.environ.get('HELLO'), 'WORLD')
if __name__ == '__main__':
subprocess.Popen(['nosetests', sys.argv[0]],
env={'HELLO': 'WORLD'}).wait()
However, this feels like a kludge, and I'd still be interested in learning to use nose.run()
properly.
os.environ
if you're doing something else after the tests ran, but this is something that can be factored into a method, I guess. I'll accept this as an answer. – Subtrahend