2
votes

I'm currently testing a wrapper to an API with PHPUnit (CLI). Due to the nature of the tests, I can pretty much use the same code for testing two different use cases. The only difference is in the values I send to the API as parameters.

So, I decided to write a class DefaultTest, where I test the API using default values used by the API and a second CustomTest(Case) where I test my parameter container with differing values. CustomTest inherites from DefaultTest, as all the functions used for validating the returned data are equivalent in both cases.

Here's some code for your understanding:

class DefaultTest extends PHPUnit_Framework_TestCase {

    public function testAPIMethod()
    {
        $this->checkParameterContainer();
        $this->validateResults();
    }

    public function checkParameterContainer() 
    {
        /* Set up default parameter container */
    }

    public function validateResults() 
    {
        /* Validate the results */
    }
}

class CustomTest extends DefaultTest {

    public function checkParameterContainer() 
    {
        /* Set up custom parameter container */
    }

    public function validateResults() 
    {
        parent::validateResult();
    }
}

PHPUnit takes the child class, executes the testAPIMethod, leading to CustomTest::checkParameterContainer() and DefaultTest::validateResults() being executed.

But DefaultTest's testAPIMethod is never executed, as DefaultTest::checkParameterContainer() is never called.

Both classes are fully valid TestCases and DefaultTest is executed normally when not specialized.

So, my question for you guys: Why is that? Do I miss something here? Is this by design?

1
It appears only public functions are being called by design.Adder
Argh... Correcting my sample code (again...) All functions are actually public.PvB
Why are you doing two classes for different test cases rather than using a dataprovider with the different data for each one? SInce the data is the only thing changing, you are able to reuse the code completely.Schleis
Sorry for coming back to your comment so late. You're right, this is actually a much cleaner approach to the problem in general. I'll adopt my tests accordingly. Thanks for your feedback. Still, I wonder why my test show this kind of behavior, as it actually should work...PvB

1 Answers

0
votes

In case, somebody needs it: PHPUnit uses reflection to find test methods on each class, but will not search in parent classes. So the following will be needed in the child test class:

public function testAPIMethod()
{
    parent::testAPIMethod();
}