Blame | Letzte Änderung | Log anzeigen | RSS feed
<html><head><META http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>SimpleTest for PHP group test documentation</title><link rel="stylesheet" type="text/css" href="docs.css" title="Styles"></head><body><div class="menu_back"><div class="menu"><h2><a href="index.html">SimpleTest</a></h2><ul><li><a href="overview.html">Overview</a></li><li><a href="unit_test_documentation.html">Unit tester</a></li><li><span class="chosen">Group tests</span></li><li><a href="mock_objects_documentation.html">Mock objects</a></li><li><a href="partial_mocks_documentation.html">Partial mocks</a></li><li><a href="reporter_documentation.html">Reporting</a></li><li><a href="expectation_documentation.html">Expectations</a></li><li><a href="web_tester_documentation.html">Web tester</a></li><li><a href="form_testing_documentation.html">Testing forms</a></li><li><a href="authentication_documentation.html">Authentication</a></li><li><a href="browser_documentation.html">Scriptable browser</a></li></ul></div></div><h1>Group Test documentation</h1><div class="content"><p><a class="target" name="group"><h2>Grouping tests</h2></a></p><p>To run test cases as part of a group the test cases should reallybe placed in files without the runner code...<pre><strong><?phprequire_once('../classes/io.php');class FileTester extends UnitTestCase {...}class SocketTester extends UnitTestCase {...}?></strong></pre>As many cases as needed can appear in a single file.They should include any code they need, such as the librarybeing tested, but none of the simple test libraries.</p><p>If you have extended any test cases, you can include themas well.<pre><?phprequire_once('../classes/io.php');<strong>class MyFileTestCase extends UnitTestCase {...}SimpleTest::ignore('MyFileTestCase');</strong>class FileTester extends MyFileTestCase {...}class SocketTester extends UnitTestCase {...}?></pre>The <span class="new_code">FileTester</span> class doesnot contain any actual tests, but is a base class for othertest cases.For this reason we use the<span class="new_code">SimpleTestOptions::ignore()</span> directiveto tell the upcoming group test to ignore it.This directive can appear anywhere in the file and workswhen a whole file of test cases is loaded (see below).We will call this sample <em>file_test.php</em>.</p><p>Next we create a group test file, called say <em>group_test.php</em>.You will think of a better name I am sure.We will add the test file using a safe method...<pre><?phprequire_once('simpletest/unit_tester.php');require_once('simpletest/reporter.php');<strong>require_once('file_test.php');$test = &new GroupTest('All file tests');$test->addTestCase(new FileTestCase());$test->run(new HtmlReporter());</strong>?></pre>This instantiates the test case before the test suite isrun.This could get a little expensive with a large number of testcases, so another method is provided that will onlyinstantiate the class when it is needed...<pre><?phprequire_once('simpletest/unit_tester.php');require_once('simpletest/reporter.php');require_once('file_test.php');$test = &new GroupTest('All file tests');<strong>$test->addTestClass('FileTestCase');</strong>$test->run(new HtmlReporter());?></pre>The problem with this method is that for every test casethat we add we will haveto <span class="new_code">require_once()</span> the test codefile and manually instantiate each and every test case.We can save a lot of typing with...<pre><?phprequire_once('simpletest/unit_tester.php');require_once('simpletest/reporter.php');$test = &new GroupTest('All file tests');<strong>$test->addTestFile('file_test.php');</strong>$test->run(new HtmlReporter());?&gt;</pre>What happens here is that the <span class="new_code">GroupTest</span>class has done the <span class="new_code">require_once()</span>for us.It then checks to see if any new test case classeshave been created by the new file and automatically addsthem to the group test.Now all we have to do is add each new file.</p><p>There are two things that could go wrong and which require care...<ol><li>The file could already have been parsed by PHP and so nonew classes will have been added. You should makesure that the test cases are only included in this fileand no others.</li><li>New test case extension classes that get included will beplaced in the group test and run also.You will need to add a <span class="new_code">SimpleTestOptions::ignore()</span>directive for these classes or make sure that they are includedbefore the <span class="new_code">GroupTest::addTestFile()</span>line.</li></ol></p><p><a class="target" name="higher"><h2>Higher groupings</h2></a></p><p>The above method places all of the test cases into one large group.For larger projects though this may not be flexible enough; youmay want to group the tests in all sorts of ways.</p><p>To get a more flexible group test we can subclass<span class="new_code">GroupTest</span> and then instantiate it as needed...<pre><?phprequire_once('simpletest/unit_tester.php');require_once('simpletest/reporter.php');<strong>class FileGroupTest extends GroupTest {function FileGroupTest() {$this->GroupTest('All file tests');$this->addTestFile('file_test.php');}}</strong>?></pre>This effectively names the test in the constructor and thenadds our test cases and a single group below.Of course we can add more than one group at this point.We can now invoke the tests from a separate runner file...<pre><?phprequire_once('file_group_test.php');<strong>$test = &new FileGroupTest();$test->run(new HtmlReporter());</strong>?></pre>...or we can group them into even larger group tests...<pre><?phprequire_once('file_group_test.php');<strong>$test = &new BigGroupTest('Big group');$test->addTestCase(new FileGroupTest());$test->addTestCase(...);$test->run(new HtmlReporter());</strong>?></pre>If we still wish to run the original group test and wedon't want all of these little runner files, we canput the test runner code around guard bars when we createeach group.<pre><?phpclass FileGroupTest extends GroupTest {function FileGroupTest() {$this->GroupTest('All file tests');$test->addTestFile('file_test.php');}}<strong>if (! defined('RUNNER')) {define('RUNNER', true);</strong>$test = &new FileGroupTest();$test->run(new HtmlReporter());}?></pre>This approach requires the guard to be set when includingthe group test file, but this is still less hassle thanlots of separate runner files.You include the same guard on the top level tests to make surethat <span class="new_code">run()</span> will run once onlyfrom the top level script that has been invoked.<pre><?php<strong>define('RUNNER', true);</strong>require_once('file_group_test.php');$test = &new BigGroupTest('Big group');$test->addTestCase(new FileGroupTest());$test->addTestCase(...);$test->run(new HtmlReporter());?></pre>As with the normal test cases, a <span class="new_code">GroupTest</span> canbe loaded with the <span class="new_code">GroupTest::addTestFile()</span> method.<pre><?phpdefine('RUNNER', true);$test = &new BigGroupTest('Big group');<strong>$test->addTestFile('file_group_test.php');$test->addTestFile(...);</strong>$test->run(new HtmlReporter());?></pre></p><p><a class="target" name="legacy"><h2>Integrating legacy test cases</h2></a></p><p>If you already have unit tests for your code or are extending externalclasses that have tests, it is unlikely that all of the test casesare in SimpleTest format.Fortunately it is possible to incorporate test cases from otherunit testers directly into SimpleTest group tests.</p><p>Say we have the following<a href="http://sourceforge.net/projects/phpunit">PhpUnit</a>test case in the file <em>config_test.php</em>...<pre><strong>class ConfigFileTest extends TestCase {function ConfigFileTest() {$this->TestCase('Config file test');}function testContents() {$config = new ConfigFile('test.conf');$this->assertRegexp('/me/', $config->getValue('username'));}}</strong></pre>The group test can recognise this as long as we includethe appropriate adapter class before we add the testfile...<pre><?phprequire_once('simpletest/unit_tester.php');require_once('simpletest/reporter.php');<strong>require_once('simpletest/adapters/phpunit_test_case.php');</strong>$test = &new GroupTest('All file tests');<strong>$test->addTestFile('config_test.php');</strong>$test->run(new HtmlReporter());?></pre>There are only two adapters, the other is for the<a href="http://pear.php.net/manual/en/package.php.phpunit.php">PEAR</a>1.0 unit tester...<pre><?phprequire_once('simpletest/unit_tester.php');require_once('simpletest/reporter.php');<strong>require_once('simpletest/adapters/pear_test_case.php');</strong>$test = &new GroupTest('All file tests');<strong>$test->addTestFile('some_pear_test_cases.php');</strong>$test->run(new HtmlReporter());?></pre>The PEAR test cases can be freely mixed with SimpleTestones even in the same test file,but you cannot use SimpleTest assertions in the legacytest case versions.This is done as a check that you are not accidently makingyour test cases completely dependent on SimpleTest.You may want to do a PEAR release of your library for examplewhich would mean shipping it with valid PEAR::PhpUnit testcases.</p></div><div class="copyright">Copyright<br>Marcus Baker, Jason Sweat, Perrick Penet 2004</div></body></html>