3

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?

2 Answers 2

6

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);
}
2
  • Fantastic, that will do the job nicely. Thank you.
    – Stephen RC
    Commented Jun 30, 2011 at 4:04
  • Cool feature I never knew about Commented Jun 30, 2011 at 14:04
1

If the documentation for a particular class or method is missing, that would represent a problem with that class, not with the method to retrieve the documentation.

Although it is probably easier to combine all of the documentation into a single test, that does not follow unit testing best practices (and hence is why the PHPUnit framework is working against you rather than for you).

I would suggest one of two approaches to rectify the issue:

  1. Refactor your tests so that each class and/or method has its own documentation check (there are a few ways you can run multiple tests in one execution).
  2. Use a tool such as PHP_CodeSniffer instead, as lack of documentation could be considered a convention – rather than a functional – defect.
5
  • My aim is to build an automated warning system for when the developer has forgotten to add documentation for an API command. Having to manually write a test to check for documentation defeats the point - it's the same as taking the time to write the documentation itself.
    – Stephen RC
    Commented Jun 29, 2011 at 4:17
  • PHP_CodeSniffer imho the tool of choice for your job @Valorin. It will automatically complain about missing doc blocks without you having to write that for yourself. (Ab)Using PHPUnit for this also works but the suggestion is definitely valid :)
    – edorian
    Commented Jun 30, 2011 at 7:26
  • Oh! I'm not talking about documentation in the code, I'm talking about user documentation - basically I have a .ini file with documentation on each command, and I have an interface which automatically generates HTML documentation pages based off the commands and the documentation in the .ini file.
    – Stephen RC
    Commented Jun 30, 2011 at 22:59
  • So it's making sure that the generated HTML user documentation is complete by checking that each API command has a corresponding block of text in the docs.ini file.
    – Stephen RC
    Commented Jun 30, 2011 at 23:00
  • 1
    If your system will automatically generate documentation, can it also generate unit tests?
    – user212218
    Commented Jul 1, 2011 at 2:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.