[TIP] Seeking testing framework for code that cannot be tested by Python unittest

Goldberg, Arthur P arthur.p.goldberg at mssm.edu
Tue Jun 28 12:15:50 PDT 2016


Thanks Rob

I solved the problem, although I took a different approach. The key insight was that I was able to pass a reference to a unittest.TestCase instance to a TestSimulationObject which then used the reference to call assert statements.

This is the code’s structure:

# this code is part of the system under test:
class SimulationObject(object):

    def __init__( self ):
        # register object with the SimulationEngine
        self.register()

    def handle_event( self, event_list ):
        # handle simulation events
        ...

class SimulationEngine(object):
    # simulation_objects: A dict of all the simulation objects, indexed by name
    simulation_objects={}

    @staticmethod
    def simulate( end_time ):
        ...
        while SimulationEngine.time <= end_time:
            ...
            # use simulation_objects to access simulation objects, and dispacts the next event(s)
            next_sim_obj.handle_event( next_sim_obj.event_queue.next_events() )

# this is test code:
class TestSimulationObject(SimulationObject):

    def handle_event( self, event_list ):
        TestSimulationObject.TestCaseRef.assertEqual( ... )

class TestSimulation(unittest.TestCase):

    def testSimulation( self ):

        TestSimObj = TestSimulationObject( 'TestSimObj' )
        # give TestSimulationObject.TestCaseRef a reference to this unittest.TestCase:
        TestSimulationObject.TestCaseRef = self

        ...
        SimulationEngine.simulate( 5.0 )

unittest.main()

On Jun 27, 2016, at 7:57 PM, Robert Collins <robertc at robertcollins.net<mailto:robertc at robertcollins.net>> wrote:
On 24 June 2016 at 04:26, Goldberg, Arthur P <arthur.p.goldberg at mssm.edu<mailto:arthur.p.goldberg at mssm.edu>> wrote:
Hi Rob

Yes, I have that impression about unittest, but could not locate
documentation which teaches me how to use it to test my code.

The system I want to test is a discrete event simulator. Its primary
components are a simulation engine and a set of simulation objects which
represent the components of the system being modeled.
The initialization of a simulation program creates the simulation objects,
each of which registers with the engine. Initialization must also create at
least one event. Events are stored in the engine. The simulation is started
by executing the engine’s scheduler method.
The scheduler repeatedly chooses the next event and calls the event handler
in the simulation object that receives the event. The event handler can
schedule future events for any simulation object, including itself. The
simulation ends when it runs out of events to process, exceeds a termination
time, or fails.
My tests use some real simulation objects and some test simulation objects.
They exchange events with each other. I want the test objects to verify
assertions about the state of the events they receive from the real
simulation objects.

I can show you code if that would help.

I'd write some code to setup the simulation ready to go, and to clean
up afterwards, and call that code from my tests. If raising an
exception from within the simulator will propogate out, then you can
use regular assert statements as well as any other exceptions you
desire to signal failure. You can also introspect your test objects
after the simulation finished to see if they detected an error.

-Rob

---

Arthur Goldberg, PhD
Associate Professor of Genetics and Genomic Sciences
Icahn Institute for Genomics & Multiscale Biology
Icahn School of Medicine at Mount Sinai
646 526 5020
Karr Lab<http://www.karrlab.org/>
Arthur.Goldberg at mssm.edu<mailto:Arthur.Goldberg at mssm.edu>
Follow us on Twitter @IcahnInstitute<https://twitter.com/IcahnInstitute>




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20160628/ade811f0/attachment.html>


More information about the testing-in-python mailing list