[Pyunit-interest] Avoiding side-effects in differnet tests
Brought to you by:
purcell
From: Jason S. <jh...@oe...> - 2004-07-01 08:59:42
|
Hi. I started using pyunit, and I ran into a worrisome problem. A side-effect had occured from a previous test (the module function acquired a threading.Lock, then raised an exception without releasing). That is causing a problem in a later test. Until now, I had assumed that each unit test was operating in a clean environment. Maybe some code will help: class ATest(unittest.TestCase): def testA(self): """This test will cause a side-effect""" import myBuggyModule assert myBuggyModule.functionWithSideEffect() def testB(self): """This test will fail becaus of testA""" import myBuggyModule assert myBuggyModule.someOtherFunction() What I had assumed is that by putting the "import myBuggyModule" statement in each test, it would get re-evaluated. But now it looks like that is not the case, and Python uses a previously-evaluated module and just imports it into the test method's namespace. This feels wrong to me, because each test case should run in a pristine environment. That is especially so since the purpose of test cases is to cause problems and detect exceptions. I think if certain module globals need to be set, they should be set in the test case, or in the setUp() method. Any comments or advice are appreciated. Thanks much. -- Jason Smith Open Enterprise Systems Bangkok, Thailand http://oes.co.th |