To run test cases as part of a group, the test cases should really
be placed in files without the runner code...
If you have extended any test cases, you can include them
as well. In PHP 4...
FileTester class does
not contain any actual tests, but is a base class for other
test cases.
For this reason we use the
SimpleTestOptions::ignore() directive
to tell the upcoming group test to ignore it.
This directive can appear anywhere in the file and works
when a whole file of test cases is loaded (see below).
If you are using PHP 5, you do not need this special directive at all.
Simply mark any test cases that should not be run as abstract...
We will call this sample file_test.php. Next we create a group test file, called say my_group_test.php. You will think of a better name I am sure.
We will add the test file using a safe method...
The main problem is that for every test case
that we add we will have
to require_once() the test code
file and manually instantiate each and every test case.
We can save a lot of typing with...
TestSuite
class has done the require_once()
for us.
It then checks to see if any new test case classes
have been created by the new file and automatically adds
them to the group test.
Now all we have to do is add each new file.
No only that, but you can guarantee that the constructor is run just before the first test method and, in PHP 5, the destructor is run just after the last test method.
There are two things that could go wrong and which require care...
SimpleTestOptions::ignore()
directive for these classes, or make sure that they are included
before the TestSuite::addTestFile()
line, or make sure that they are abstract classes.
The above method places all of the test cases into one large group. For larger projects though this may not be flexible enough; you may want to group the tests in all sorts of ways.
To get a more flexible group test we can subclass
TestSuite and then instantiate it as needed...
If we still wish to run the original group test, and we
don't want all of these little runner files, we can
put the test runner code around guard bars when we create
each group.
run() will run once only
from the top level script that has been invoked.
TestSuite can
be loaded with the TestSuite::addTestFile() method.
If you already have unit tests for your code or are extending external classes that have tests, it is unlikely that all of the test cases are in SimpleTest format. Fortunately it is possible to incorporate test cases from other unit testers directly into SimpleTest group tests.
Say we have the following
PhpUnit
test case in the file config_test.php...