Summary
How can I create a base class that extends PHPUnit_Framework_TestCase and use that for subclassing actual test cases, without having the base class itself tested by PHPUnit?
Further explanation
I have a series of related test cases for which I have created a base class that contains some common tests to be inherited by all test cases:
BaseClass_TestCase.php: class BaseClass_TestCase extends PHPUnit_Framework_TestCase { function test_common() { // Test that should be run for all derived test cases } } MyTestCase1Test.php: include 'BaseClass_TestCase.php'; class MyTestCase1 extends BaseClass_TestCase { function setUp() { // Setting up } function test_this() { // Test particular to MyTestCase1 } } MyTestCase2Test.php: include 'BaseClass_TestCase.php'; class MyTestCase2 extends BaseClass_TestCase { function setUp() { // Setting up } function test_this() { // Test particular to MyTestCase2 } }
My problem is that when I try to run all the tests in the folder, it fails (without output).
Trying to debug I’ve found that the problem lies with the base class being itself a subclass of PHPUnit_Framework_TestCase and therefore PHPUnit will also try to run its tests. (Until then I naively thought that only classes defined inside actual test files – filenames ending in Test.php – would be tested.)
Running the base class as a test case out of context doesn’t work due to details in my specific implementation.
How can I avoid the base class being tested, and only test the derived classes?
Advertisement
Answer
Make it abstract, PHPUnit should ignore it.