I am trying to implement a Django like test utility for a php application using PHPUnit. By Django like, I mean a separate test db is created from the main database before running the first test and it's dropped after running the last test. The test db needs to be created only once even if many test cases are run at a time.
For this, I took the following approach -
I defined a custom test suite class so that I can write the code for creating and dropping the db in it's setup and teardown methods and then use this class to run the tests as follows
$ phpunit MyTestSuite
MyTestSuite defines a static method named suite
where I just use glob
and add tests to the testsuite as follows
public static function suite() {
$suite = new MyTestSuite();
foreach (glob('./tests/*Test.php') as $tc) {
require_once $tc;
$suite->addTestSuite(basename($tc, '.php'));
}
return $suite;
}
All Test Case classes extend from a subclass of PHPUnit_Framework_TestCase
and the setup and teardown methods of this class take care of loading and clearing initial data from json fixture files.
Now as the no. of tests are increasing, I need to run only a selected tests at a time. But since I am already loading tests using a test suite, the --filter option cannot be used. This makes me feel that this approach may not have been the correct one.
So my question is, what is the correct approach to do something before running the first test and after running the last test irrespective of how PHPUnit finds them ?
PS: I am not using PHPUnit_Extensions_Database_TestCase but my own implementation of creating, populating and dropping the db.
setUpBeforeClass
andtearDownAfterClass
respectively... But for your case. The whole approach looks like it violates one of the major requirements for unit tests: isolation. All the tests should be isolated from each other, and it implies loading fixtures before each test case – zerkms