Python unit-testing with the nose: sequential tests

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

( The project I'm writing tests on is a simulation system, and, among other things, allows you to run simulations both synchronously and asynchronously, and the simulation results should be the same in both.)

The fact is that some of my tests use simulation results that were created in other tests. For example, it synchronous_testcalculates a specific simulation in synchronous mode, but then I want to calculate it in asynchronous mode and verify that the results are the same.

How do I structure this? Do I put them all in one test function or create a separate one asynchronous_test? Am I passing these objects from one test function to another?

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

+3
source share
2 answers

You can add tests that need to be calculated once in a class to "tune" this 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)
+6
source

, . synchronous_test, _test, , .

- :

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)
+6

Source: https://habr.com/ru/post/1738811/


All Articles