1
votes

I'm using PyDev and testing with nose. Is there a way I can set an environment variable inside of PyDev only when I run unit tests?

thanks

1
Can you not set the variable in your test setup and then reset it in your teardown? - JAB
Well, probably. That's the less attractive alternative since I'd have to do that in every unit test. - piyo
No you wouldn't, nose supports lots of levels/types of test fixtures. You can even do package-level: nose.readthedocs.org/en/latest/writing_tests.html#fixtures And even without nose, you could always create a subclass of unittest.TestCase that performs the necessary work in its setUp and tearDown methods and then use that for your test classes (for that matter, the current version of unittest provides multiple test fixture layers as well). - JAB
Awesome. I feel kind of dumb for not suspecting such a thing existed. Thanks. - piyo

1 Answers

0
votes

Moving my comments into a proper answer: As nose supports multiple levels of test fixtures, even package-level, and even without nose unittest.TestCase supports setUp() and tearDown() methods (and starting with Python 3.2, setUpClass() and tearDownClass() as class methods, as well as the module-level setUpModule() and tearDownModule()), it's easy to perform common initialization and post-testing operations such as setting environment variables, etc. without having to place the necessary code within each test function or even having to explicitly call the test fixture functions.