I am developing an API documentation system, and want to dynamically check that each command has documentation attached. The easiest way to do this is dynamically loop through each command and check for existing documentation to match it.

My code looks like this:

public function testMissingDocs()
{
    foreach ($aCommands as $sKey => $aOptions)
    {
        $this->assertNotNull($oDocs->get($sKey));
    }
}

The problem with this is the StopOnFailure/Error feature of PHPUnit which stops the test after the first assertion fails. I understand the reasons for this functionality and I want to keep it on for the majority of my test cases, but for dynamic assertions/tests it makes things a bit hard.

Is there a way to disable it on a per-test basis so I can check each command in this test?

Accepted Answer

You can use a data provider to split the single test into as many tests as you have commands.

/**
 * @dataProvider getDocsForAllCommands
 */
public function testEveryCommandHasDocs($sKey)
{
    $this->assertNotNull($oDocs->get($sKey));
}

public function getKeysForAllCommands()
{
    return array_keys($aCommands);
}
Written by David Harkness
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki