2

I'm running some tests and some reports don't make sense for example:

enter image description here

enter image description here

I also have another example: Why xdebug marks 587 and 588 as not executed and 589 as executed?

Andrei Lupuleasa
  • 2,677
  • 3
  • 14
  • 32

2 Answers2

2

If you don't have a test where switch is non–matching then xdebug coverage is correct.

If execution is only ever returned from switch in tests then end of method (implicit void return) is never ever reached/executed and as such not covered.

You can be confident that at this time there is no problem here, but over time this is brittle. What if you add a third possibility, but forget to handle it? And so on.

Coverage does its job here — it points out a (potential) code path that isn’t being tested.

Rarst
  • 2,335
  • 16
  • 26
1

By looking at the example you provided xdebug is actually "right", since the return statements are all inside the switch, you never get to the last line of that function.

Also there are options to allow "strict" coverage, to exclude code invoked recursively, so be careful you are not running some of these if it's not the behaviour you are expecting

Riccardo Cedrola
  • 1,270
  • 1
  • 10
  • 16
  • So if code is executed recursively I have to coverage all conditions on every recursion? – Andrei Lupuleasa Feb 21 '19 at 16:07
  • 1
    Let me try to explain: if you have tests for the method `methodTested()` you will see that method as green in your xdebug report. If the `methodTested()` calls another method called like `methodNotTested()` you could expect the lines of this method to be red inside the xdebug report, but they are actually going to be green, because for xdebug the code has been run and since no errors where thrown, it is considered "safe". So a 100% coverage of a class/method is not itself a reason to consider the code bugproof – Riccardo Cedrola Feb 21 '19 at 16:10
  • 1
    My advice is always to use xdebug as a tool while covering the code, don't follow it religiously, don't hassle too much to get that 100% code coverage. Just cover the important stuff and use xdebug to ensure you covered each scenario – Riccardo Cedrola Feb 21 '19 at 16:15
  • What about when `methodTested()` recalls itself and on recursion not all case from inside of it are covered? – Andrei Lupuleasa Feb 21 '19 at 16:25
  • 1
    I can't give you a sure answer about that but my instinct says that the reports are going to be merged, so you will see everything green even if the recursion didn't cover all the scenarios. Btw if you ask me is not a problem, once you are sure that a method behaves as expected, you could also assure that on recursion it will work the same way. Don't know if this makes sense to you – Riccardo Cedrola Feb 21 '19 at 16:32
  • Can you explain this https://stackoverflow.com/questions/54828254/why-xdebug-marks-lines-as-not-executed ? – Andrei Lupuleasa Feb 22 '19 at 13:33