Unit Testing -- a Simple Example

From Joomla! Documentation

Revision as of 16:49, 4 March 2008 by Instance (Talk | contribs)

Anatomy of a Basic Test

Here we're going to look at a very simple test for the JRequest class.

First there's a block of code that's cut from the sample templates:

if (!defined('JUNIT_MAIN_METHOD')) {
        define('JUNIT_MAIN_METHOD', 'JRequestTest_GetMethod::main');
        $JUnit_home = DIRECTORY_SEPARATOR . 'unittest' . DIRECTORY_SEPARATOR;
        if (($JUnit_posn = strpos(__FILE__, $JUnit_home)) === false) {
                die('Unable to find ' . $JUnit_home . ' in path.');
        $JUnit_posn += strlen($JUnit_home) - 1;
        $JUnit_root = substr(__FILE__, 0, $JUnit_posn);
        $JUnit_start = substr(
                $JUnit_posn + 1,
                strlen(__FILE__) - strlen(basename(__FILE__)) - $JUnit_posn - 2
        require_once $JUnit_root . DIRECTORY_SEPARATOR . 'setup.php';

The only part that's customized here is JRequestTest_GetMethod, which is the name of the test class. This code corresponds to some at the end of the file. Together, this allows you to run the test directly from the command line. Here's the code at the bottom:

// Call main() if this source file is executed directly.
if (JUNIT_MAIN_METHOD == 'JRequestTest_GetMethod::main') {

You can see the same thing, the name of the test class is all that's different from the template.

(to be continued)