<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">
Thanks Rob
<div><br>
</div>
<div>I solved the problem, although I took a different approach. The key insight was that I was able to pass a reference to a&nbsp;<span style="font-family: Monaco;">unittest.TestCase</span>&nbsp;instance to a&nbsp;<span style="font-family: Monaco;">TestSimulationObject</span>&nbsp;which
 then used the reference to call assert statements.</div>
<div><br>
</div>
<div>This is the code’s structure:</div>
<div><br>
</div>
<div>
<div><font face="Monaco"># this code is part of the system under test:</font></div>
<div><font face="Monaco">class SimulationObject(object):</font></div>
<div><font face="Monaco"><br>
</font></div>
<div><font face="Monaco">&nbsp; &nbsp; def __init__( self ):</font></div>
<div><font face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp; # register object with the SimulationEngine</font></div>
<div><font face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp; self.register()</font></div>
<div><font face="Monaco"><br>
</font></div>
<div><font face="Monaco">&nbsp; &nbsp; def handle_event( self, event_list ):</font></div>
<div><font face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp; # handle simulation events</font></div>
<div><font face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp; ...</font></div>
<div><font face="Monaco"><br>
</font></div>
<div><font face="Monaco">class SimulationEngine(object):</font></div>
<div><font face="Monaco">&nbsp; &nbsp; # simulation_objects: A dict of all the simulation objects, indexed by name</font></div>
<div><font face="Monaco">&nbsp; &nbsp; simulation_objects={}</font></div>
<div><font face="Monaco"><br>
</font></div>
<div><font face="Monaco">&nbsp; &nbsp; @staticmethod</font></div>
<div><font face="Monaco">&nbsp; &nbsp; def simulate( end_time ):</font></div>
<div><font face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp; ...</font></div>
<div><font face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp; while SimulationEngine.time &lt;= end_time:</font></div>
<div><font face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...</font></div>
<div><font face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # use simulation_objects to access simulation objects, and dispacts the next event(s)</font></div>
<div><font face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; next_sim_obj.handle_event( next_sim_obj.event_queue.next_events() )</font></div>
<div><font face="Monaco"><br>
</font></div>
<div><font face="Monaco"># this is test code:</font></div>
<div><font face="Monaco">class TestSimulationObject(SimulationObject):</font></div>
<div><font face="Monaco"><br>
</font></div>
<div><font face="Monaco">&nbsp; &nbsp; def handle_event( self, event_list ):</font></div>
<div><font face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp; TestSimulationObject.TestCaseRef.assertEqual( ... )</font></div>
<div><font face="Monaco"><br>
</font></div>
<div><font face="Monaco">class TestSimulation(unittest.TestCase):</font></div>
<div><font face="Monaco"><br>
</font></div>
<div><font face="Monaco">&nbsp; &nbsp; def testSimulation( self ):</font></div>
<div><font face="Monaco"><br>
</font></div>
<div><font face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp; TestSimObj = TestSimulationObject( 'TestSimObj' )</font></div>
<div><font face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp; # give TestSimulationObject.TestCaseRef a reference to this unittest.TestCase:</font></div>
<div><font face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp; TestSimulationObject.TestCaseRef = self</font></div>
<div><font face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</font></div>
<div><font face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp; ...</font></div>
<div><font face="Monaco">&nbsp; &nbsp; &nbsp; &nbsp; SimulationEngine.simulate( 5.0 )</font></div>
<div><font face="Monaco">&nbsp; &nbsp;&nbsp;</font></div>
<div><font face="Monaco">unittest.main()</font></div>
<div><br>
</div>
<div>
<div>On Jun 27, 2016, at 7:57 PM, Robert Collins &lt;<a href="mailto:robertc@robertcollins.net">robertc@robertcollins.net</a>&gt; wrote:</div>
<blockquote type="cite">On 24 June 2016 at 04:26, Goldberg, Arthur P &lt;<a href="mailto:arthur.p.goldberg@mssm.edu">arthur.p.goldberg@mssm.edu</a>&gt; wrote:<br>
<blockquote type="cite">Hi Rob<br>
<br>
Yes, I have that impression about unittest, but could not locate<br>
documentation which teaches me how to use it to test my code.<br>
<br>
The system I want to test is a discrete event simulator. Its primary<br>
components are a simulation engine and a set of simulation objects which<br>
represent the components of the system being modeled.<br>
The initialization of a simulation program creates the simulation objects,<br>
each of which registers with the engine. Initialization must also create at<br>
least one event. Events are stored in the engine. The simulation is started<br>
by executing the engine’s scheduler method.<br>
The scheduler repeatedly chooses the next event and calls the event handler<br>
in the simulation object that receives the event. The event handler can<br>
schedule future events for any simulation object, including itself. The<br>
simulation ends when it runs out of events to process, exceeds a termination<br>
time, or fails.<br>
My tests use some real simulation objects and some test simulation objects.<br>
They exchange events with each other. I want the test objects to verify<br>
assertions about the state of the events they receive from the real<br>
simulation objects.<br>
<br>
I can show you code if that would help.<br>
</blockquote>
<br>
I'd write some code to setup the simulation ready to go, and to clean<br>
up afterwards, and call that code from my tests. If raising an<br>
exception from within the simulator will propogate out, then you can<br>
use regular assert statements as well as any other exceptions you<br>
desire to signal failure. You can also introspect your test objects<br>
after the simulation finished to see if they detected an error.<br>
<br>
-Rob<br>
</blockquote>
</div>
<br>
<div>
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">
<div>
<div style="orphans: 2; widows: 2; border-collapse: separate; border-spacing: 0px;">
<div style="border-collapse: separate; border-spacing: 0px;">---</div>
<div>&nbsp;</div>
<div>Arthur Goldberg, PhD
<div style="border-collapse: separate; border-spacing: 0px;">Associate Professor of Genetics and Genomic Sciences</div>
<div style="border-collapse: separate; border-spacing: 0px;">Icahn Institute for Genomics &amp;&nbsp;Multiscale Biology</div>
<div>Icahn School of Medicine at Mount Sinai</div>
<div>
<div style="border-collapse: separate; border-spacing: 0px;">646 526 5020</div>
</div>
<div style="border-collapse: separate; border-spacing: 0px;"><a href="http://www.karrlab.org/">Karr Lab</a></div>
<div style="border-collapse: separate; border-spacing: 0px;"><a href="mailto:Arthur.Goldberg@mssm.edu">Arthur.Goldberg@mssm.edu</a></div>
</div>
</div>
<div style="orphans: 2; widows: 2;">Follow us on Twitter&nbsp;<a href="https://twitter.com/IcahnInstitute">@IcahnInstitute</a></div>
</div>
<div><br>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<br class="Apple-interchange-newline">
<br class="Apple-interchange-newline">
</div>
<br>
</div>
</body>
</html>