In order to supportability, a large departure from pyposmat to pypospack is the inclusion of unit testing scripts. At this point, this page probably includes a lot of information which should be moved to a tutorial document. However, at this point, I'm going to leave this information as one large document until I figure out where to move information.
A good explanation of python's capabilities of unit testing can be found with the following references:
The basic building block of unit testing code are test cases - single scenarios that must be set up and checked for correctness. In unittest
, test cases are created through instances of the unittest.TestCase
instances. To make test cases either the TestCase
or FunctionTestCase
must be subclassed.
Test cases can be organized by fixtures which they use. Test fixtures are methods and functions used before and after a test. This sets up the preconditions needed before the test and cleanup after the test is completed. Preconditioning activities are typically set up with the setUp()
method to allocate, open, or connect to some resource. Cleanup, typically set up with the tearDown()
method deallocates, closes, and disconnects from the resources. Since pypospack
manages input/output to and from different calculators, these activities will be included in the the tearDown process.
import unittest
class WidgetTestCase(unittest.TestCase):
def setUp(self):
self.widget = Widget('The widget')
def test_default_widget_size(self):
self.assertEqual(self.widget.size(), (50,50), 'incorrect default size')
def test_widget_resize(self):
self.widget.resize(100,150)
self.assertEqual(self.widget.size(), (100,150), 'wrong size after resize')
def tearDown(self):
# some stuff here
To run this these tests
python -m unittest -q widgettestcase.py
Definitions of test cases and test suites are placed in a separate module. This allows the tests to be run in standalone from the command line. The test code can be separated from the production code.
assertTrue(x, msg=None)
assertFalse(x, msg=None)
assertIsNone(x, msg=None)
assertIsNotNone(x, msg=None)
assertEqual(a, b, msg=None)
assertNotEqual(a, b, msg=None)
assertIs(a, b, msg=None)
assertIsNot(a, b, msg=None)
assertIn(a, b, msg=None)
assertNotIn(a, b, msg=None)
assertIsInstance(a, b, msg=None)
assertNotIsInstance(a, b, msg=None)