Actions

Difference between revisions of "Unit Testing -- a Simple Example"

From Joomla! Documentation

(New page: 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: <nowiki> if (!defined('JUNIT_MAIN_METHOD')) { ...)
 
Line 1: Line 1:
 +
== Anatomy of a Basic Test ==
 
Here we're going to look at a very simple test for the JRequest class.
 
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:
 
First there's a block of code that's cut from the sample templates:
  
<nowiki>
+
<pre>
 
if (!defined('JUNIT_MAIN_METHOD')) {
 
if (!defined('JUNIT_MAIN_METHOD')) {
 
define('JUNIT_MAIN_METHOD', 'JRequestTest_GetMethod::main');
 
define('JUNIT_MAIN_METHOD', 'JRequestTest_GetMethod::main');
Line 19: Line 20:
 
require_once $JUnit_root . DIRECTORY_SEPARATOR . 'setup.php';
 
require_once $JUnit_root . DIRECTORY_SEPARATOR . 'setup.php';
 
}
 
}
</nowiki>
+
</pre>
  
 
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:
 
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:
<nowiki>
+
<pre>
 
// Call main() if this source file is executed directly.
 
// Call main() if this source file is executed directly.
 
if (JUNIT_MAIN_METHOD == 'JRequestTest_GetMethod::main') {
 
if (JUNIT_MAIN_METHOD == 'JRequestTest_GetMethod::main') {
 
JRequestTest_GetMethod::main();
 
JRequestTest_GetMethod::main();
 
}
 
}
</nowiki>
+
</pre>
 +
 
 +
You can see the same thing, the name of the test class is all that's different from the template.
 +
 
 +
(to be continued)

Revision as of 15:49, 4 March 2008

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(
                __FILE__,
                $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') {
        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)