Unit TestingΒΆ

In this notebook we’ll demonstrate some basic unit testing for models.

We build on the standard python unittest libarary, which is documented here. Other testing suites are available, with their own advantages, but this is the most basic, so we’ll use it for our demonstration.

Unit testing us usually done not in the ipython notebook, but in a standalone python file. Tests for this demo are found in the accompanying file testsite.py:

import pysd
import unittest

class TestTeacupModel(unittest.TestCase):
    """ Test Import functionality """
    @classmethod
    def set_up_class(cls):
        cls.model = pysd.read_vensim('../../models/Teacup/Teacup.mdl')

    def test_initialization(self):
        self.assertEqual(self.model.components.state['teacup_temperature'],180.0)

    def test_heatflow_calc(self):
        self.assertEqual(self.model.components.heat_loss_to_room(), 11.0)
        
    def test_output(self):
        self.assertAlmostEqual(self.model.run()['teacup_temperature'].iloc[-1], 
                               75, delta=1)

if __name__ == '__main__':
    unittest.main()
%run testsuite.py
...
----------------------------------------------------------------------
Ran 3 tests in 0.053s

OK