views:

913

answers:

5

My test file is basically:

class Test(unittest.TestCase):
    def testOk():
        pass

if __name__ == "__main__":
    expensiveSetup()
    try:
        unittest.main()
    finally:
        cleanUp()

However, I do wish to run my test through Netbeans testing tools, and to do that I need unittests that don't rely on an environment setup done in main. Looking at http://stackoverflow.com/questions/402483/caching-result-of-setup-using-python-unittest - it recommends using Nose. However, I don't think Netbeans supports this. I didn't find any information indicating that it does. Additionally, I am the only one here actually writing tests, so I don't want to introduce additional dependencies for the other 2 developers unless they are needed.

How can I do the setup and cleanup once for all the tests in my TestSuite?

The expensive setup here is creating some files with dummy data, as well as setting up and tearing down a simple xml-rpc server. I also have 2 test classes, one testing locally and one testing all methods over xml-rpc.

A: 

You can save the state if expensiveSetup() is run or not.

__expensiveSetup_has_run = False

class ExpensiveSetupMixin(unittest.TestCase):
    def setUp(self):
        global __expensiveSetup_has_run
        super(ExpensiveSetupMixin, self).setUp()
        if __expensiveSetup_has_run is False:
            expensiveSetup()
            __expensiveSetup_has_run = True

Or some kind of variation of this. Maybe pinging xml-rpc server and create a new one if it isn't answering.

But the unit-testing way AFAIK is to setup and teardown per unittest even if it is expensive.

muhuk
A: 

You can assure setUp and tearDown execute once if you have only one test method, runTest. This method can do whatever else it wants. Just be sure you don't have any methods with names that start with test.

class MyExpensiveTest( unittest.TestCase ):
    def setUp( self ):
        self.resource = owThatHurts()
    def tearDown( self ):
        self.resource.flush()
        self.resource.finish()
    def runTest( self ):
        self.runTest1()
        self.tunTest2()
    def runTest1( self ):
        self.assertEquals(...)
    def runTest2( self ):
        self.assertEquals(...)

It doesn't automagically figure out what to run. If you add a test method, you also have to update runTest.

S.Lott
That would make it harder to see which test fails, and also to just rerun 1 test.
Staale
@Staale: False -- you still know precisely where the failure occurred -- you still get a full, useful traceback. Rerunning just one test is done by tweaking the code. That's the way we do it -- because it so rarely happens that I want to run one test.
S.Lott
+1  A: 

First of all, what S. Lott said. However!, you do not want to do that. There is a reason setUp and tearDown are wrapped around each test: they help preserve the determinism of testing.

Otherwise, if some test places the system in a bad state, your next tests may fail. Ideally, each of your tests should be independent.

Also, if you insist on doing it this way, instead of writing by hand self.runTest1(), self.runTest2(), you might want to do a bit of introspection in order to find the methods to run.

A: 

Yet another idea may be to use

def setUpClass(klass):
    # ...

to perform some initialization once for all tests belonging to the given class.

Mekk
A: 

Won't package-level initialization do it for you? From the Nose Wiki:

nose allows tests to be grouped into test packages. This allows package-level setup; for instance, if you need to create a test database or other data fixture for your tests, you may create it in package setup and remove it in package teardown once per test run, rather than having to create and tear it down once per test module or test case.

To create package-level setup and teardown methods, define setup and/or teardown functions in the __init__.py of a test package. Setup methods may be named setup, setup_package, setUp, or setUpPackage; teardown may be named teardown, teardown_package, tearDown or tearDownPackage. Execution of tests in a test package begins as soon as the first test module is loaded from the test package.

Brian Clapper