23.9. Unit tests¶
VMXi includes a number of Jython unit tests. That is, a set of test cases that can be run from the Jython terminal of a running GDA instance, or offline through the normal JUnit mechanisms.
These tests are run by Jenkins.
23.9.1. Running Unit Tests¶
The tests are located in the unit_testing package in i02-2-config.
Jenkins runs all tests by running the module unit_testing.testing, which
uses testhelpers.gda_test_harness.GdaTestRunner in GDA Core.
Note that this module can not be run in this way within a running instance
of GDA, but the function unit_testing.testing.run_all_tests() can be
executed to run all test cases. E.g.:
>>> import unit_testing
>>> unit_testing.testing.run_all_tests()
... test output omitted ...
Ran 86 tests in 9.049s
OK
Individual test files can be run within a running instance of GDA by
running the relevant file. Each file includes a __name__ == "__main__":
block to facilitate this. E.g.:
>>> run("unit_testing/tasks/test_executor")
test_error_completes (__main__.TestTaskExecutor) ... ok
test_error_during_finish_stops (__main__.TestTaskExecutor) ... ok
test_error_stops_running (__main__.TestTaskExecutor) ... ok
test_failure_stops (__main__.TestTaskExecutor) ... ok
test_input_variables (__main__.TestTaskExecutor) ... ok
test_interrupt_abort_stops (__main__.TestTaskExecutor) ... ok
test_interrupt_finish (__main__.TestTaskExecutor) ... ok
test_step_no_tasks (__main__.TestTaskExecutor) ... ok
test_step_with_running (__main__.TestTaskExecutor) ... ok
test_task_completion_completes (__main__.TestTaskExecutor) ... ok
test_task_execute_called_only_once (__main__.TestTaskExecutor) ... ok
test_task_submit_called (__main__.TestTaskExecutor) ... ok
test_task_submit_with_dependencies (__main__.TestTaskExecutor) ... ok
----------------------------------------------------------------------
Ran 13 tests in 0.070s
OK
The ability to run tests in this manner has proven very useful during initial development and it is recommended that all future test files include this feature.
Care must be taken that test cases do not interfere with beamline operation or objects.
If a test case involves code that will try to use, e.g., a Lims object,
then a dummy-in-memory object must be created and used for that case without
interfering with the one setup in localStation.py, for normal beamline use.
23.9.2. VMXi Test Case¶
A VMXi Test Case class is provided under unit_testing.test_util.
This derives from unittest.TestCase, providing the additional method:
test_case.assertListAlmostEqual(first, second, places=7, msg=None, delta=None)
This method is meant to be the list-analogue to:
test_case.assertAlmostEqual(first, second, places=7, msg=None, delta=None)
Applying this method to the pairwise elements of the input objects (assumed to be lists/arrays of doubles).
class VmxiTestCase(unittest.TestCase):
def assertListAlmostEqual(self, first, second, places=7, msg=None, delta=None):
# run self.assertEqual(first, second), then self.assertAlmostEqual for each pair wise element of first and second