|
|
|
|
Unit tester
|
Simple Test PHP Unit Test Framework
|
Mock objects
|
Group tests
Table of Contents
Grouping tests into suites
There are many ways to group tests together into test suites.
One way is to simply place multiple test cases into a single file...
<?php
require_once(dirname(__FILE__) . '/simpletest/autorun.php');
require_once(dirname(__FILE__) . '/../classes/io.php');
...
}
...
}
?>
As many cases as needed can appear in a single file.
They should include any code they need, such as the library
being tested, but need none of the SimpleTest libraries.
If you have extended any test cases, you can include them too.
Simply mark any test cases that should not be run as abstract...
...
}
class FileTester extends MyFileTestCase { ... }
Here the FileTester class does
not contain any actual tests, but is a base class for other
test cases.
We will call this sample file_test.php.
Next we create a test suite file, called say my_test_suite.php.
You will think of a better name I am sure.
We will add the test file using a safe method...
<?php
require_once('simpletest/autorun.php');
require_once('file_test.php');
function __construct() {
parent::__construct();
$this->addFile('file_test.php');
}
}
?>
What happens here is that the TestSuite
class will do 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 test suite.
This method gives us the most control.
If this is too much typing, and you are willing to group
test suites together in their own directories or otherwise
tag the file names, then there is a more automatic way...
<?php
require_once('simpletest/autorun.php');
function __construct() {
parent::__construct();
$this->collect(dirname(__FILE__
) .
'/unit',
}
}
?>
This will scan a directory called "unit" for any files
ending with "_test.php" and load them.
You don't have to use SimplePatternCollector to
filter by a pattern in the filename, but this is the most common
usage.
Now all you have to do is drop a file of test cases into the
directory and it will run just by running the test suite script.
Test cases loaded with the addFile method have some
useful properties.
You can guarantee that the constructor is run
just before the first test method and the destructor
is run just after the last test method.
This allows you to place test case wide set up and tear down
code in the constructor and destructor, just like a normal
class.
Composite suites
The above method places all of the test cases into one large suite.
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...
<?php
require_once('simpletest/autorun.php');
function __construct() {
parent::__construct();
$this->addFile('file_tests.php');
}
}
?>
This effectively adds our test cases and a single suite below
the first.
Not much use as is, but we can add more than one suite at this point.
We can even mix groups and test cases freely as long as
we are careful about double includes.
<?php
require_once('simpletest/autorun.php');
function __construct() {
parent::__construct();
$this->addFile('file_tests.php');
$this->addFile('some_other_test.php');
}
}
?>
In the event of a double include, ony the first instance
of the test case will be run.
|
|
|
|
Unit tester
|
Simple Test PHP Unit Test Framework
|
Mock objects
|
Documentation generated on Thu, 01 Oct 2009 20:54:09 -0500 by phpDocumentor 1.4.2