How to get 100% Code Coverage with PHPUnit

后端 未结 3 2035
萌比男神i
萌比男神i 2021-01-20 01:03

I am writing a Zend Framework application and unit testing it with PHPUnit. In general, things are going swimmingly however I have a small, but annoying issue with PHPUnit

3条回答
  •  野的像风
    2021-01-20 01:26

    The code you have only comments for is what matters. The closing brace of a block will be shown as executable in the code coverage report if it's possible to fall through to the end. Look for a branch that you aren't actually testing.

    if ($request->isPost()) {
        if ($x < 5) {
            return '<';
        }
        elseif ($x > 5) {
            return '>';
        }
        // Do you have a test for $x == 5?
    }
    

    As for the 100% code coverage goal, I agree wholeheartedly with Bill. For some framework classes that I write I will strive to make 100%, but I know that doesn't mean I've truly tested every possibility. Often, when I find myself working overly hard to achieve 100% coverage, it's probably OCD kicking in. :)

    Just . . . one . . . more . . . test . . .

提交回复
热议问题