Search code examples
pythonunit-testingnose

Python unit-testing with nose: Making sequential tests


I am just learning how to do unit-testing. I'm on Python / nose / Wing IDE.

(The project that I'm writing tests for is a simulations framework, and among other things it lets you run simulations both synchronously and asynchronously, and the results of the simulation should be the same in both.)

The thing is, I want some of my tests to use simulation results that were created in other tests. For example, synchronous_test calculates a certain simulation in synchronous mode, but then I want to calculate it in asynchronous mode, and check that the results came out the same.

How do I structure this? Do I put them all in one test function, or make a separate asynchronous_test? Do I pass these objects from one test function to another?

Also, keep in mind that all these tests will run through a test generator, so I can do the tests for each of the simulation packages included with my program.


Solution

  • You can add tests that need to calculate once per class to the "setup" of that class. As an example:

    from nose.tools import *
    class Test_mysim():
        def setup(self):
            self.ans = calculate_it_once()
    
        def test_sync(self):
            ans=calculate_it_sync()
            assert_equal(ans,self.ans)
    
        def test_async(self):
            ans=calculate_it_async()
            assert_equal(ans,self.ans)