Developer Unit Tests

Table of Contents:


Overview
Writing a test
Example Code
  • Overview

    The Developer Unit Tests module is for developers. Developers can write tests that get run against their modules to check that all is continuing to run as they expect it should be.

    Read more about "Unit Testing" here at wikipedia
  • screenshot of the Run Unit Tests tab of the Developer Unit Tests module in the ACP
  • screenshot of a failed unit test during a testing run
  • screenshot of the a successful run of the unit tests, no errors
  • Writing a test

    Tests are added to the /test folder of your module. The name of the file will show up in the list on the RUN UNIT TESTS tab of the module.

    For example the jrEmbed module has a file called embed.php in its test folder.
  • screenshot of an IDE with the location of the test shown in the module structure.
  • screenshot of the Editor Embedded Media ( jrEmbed ) unit tests in the ACP
  • Example Code

    This code is taken from the jrEmbed module ( Editor Embeded Media ). The test is given a name to show in the modal dialog using the jrUnitTest_init_test() function, then it runs the test.

    If any of the checks fails the jrUnitTest_exit_with_error() function throws the error to the modal dialog.

    If no errors are thrown, the test is deemed to be successful.
    /**
     * Cache system unit tests
     */
    function test_jrEmbed_embed()
    {
        jrUnitTest_init_test('Parse params');
        $tmp = 'module="jrEmbed" mode="place" location="Corner Brook, NL, Canada" maptype="roadmap"';
        $_rs = jrEmbed_get_param_array_from_string($tmp);
        if (!is_array($_rs)) {
            jrUnitTest_exit_with_error('failed to create array');
        }
        if (!isset($_rs['module']) || $_rs['module'] != 'jrEmbed') {
            jrUnitTest_exit_with_error('bad parsed param: module');
        }
        if (!isset($_rs['mode']) || $_rs['mode'] != 'place') {
            jrUnitTest_exit_with_error('bad parsed param: mode');
        }
        if (!isset($_rs['location']) || $_rs['location'] != 'Corner Brook, NL, Canada') {
            jrUnitTest_exit_with_error('bad parsed param: location');
        }
        if (!isset($_rs['maptype']) || $_rs['maptype'] != 'roadmap') {
            jrUnitTest_exit_with_error('bad parsed param: maptype');
        }
        /* different string */
        $tmp = 'module="jrAudio" search="audio_genre = rock"';
        $_rs = jrEmbed_get_param_array_from_string($tmp);
        if (!is_array($_rs)) {
            jrUnitTest_exit_with_error('failed to create array (2)');
        }
        if (!isset($_rs['module']) || $_rs['module'] != 'jrAudio') {
            jrUnitTest_exit_with_error('bad parsed param: module (2)');
        }
        if (!isset($_rs['search']) || $_rs['search'] != 'audio_genre = rock') {
            jrUnitTest_exit_with_error('bad parsed param: search');
        }
    
    
    }

Share This

Tags