Reputation: 88518
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.
Upvotes: 3
Views: 4724
Reputation: 4798
In general, I'd recommend not making one test depend upon another. Do the synchronous_test, do the asynchronous_test, compare them each to the expected correct output, not to each other.
So something like:
class TestSimulate(TestCase):
def setup(self):
self.simpack = SimpackToTest()
self.initial_state = pickle.load("initial.state")
self.expected_state = pickle.load("known_good.state")
def test_simulate(self):
state = simulate(self.simpack, self.initial_state)
# assert_equal will require State to implement __eq__ in a meaningful
# way. If it doesn't, you'll want to define your own comparison
# function.
self.assert_equal(self.expected_state, state)
def test_other_simulate(self):
foo = OtherThing(self.simpack)
blah = foo.simulate(self.initial_state)
state = blah.state
self.assert_equal(self.expected_state, state)
Upvotes: 6
Reputation: 21218
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)
Upvotes: 6