View Issue Details

IDProjectCategoryView StatusLast Update
0001227XdebugCode Coveragepublic2020-03-12 17:35
Reporterdavid.proweb Assigned Toderick  
PriorityhighSeverityfeatureReproducibilityalways
Status resolvedResolutionno change required 
Product Version2.3.3 
Summary0001227: Code-coverage: validate each conditional execution
Description

Currently, if you have a code like:

$value = isset($number) ? $number : 0;

And your test executes it once, it'll be marked as "code executed". Even if only one of conditional statement was really executed.

Same happen on if conditionals. It'll not check if all your code was executed correctly.

if (expression1() || // executed
expression2()) // ignored (even when executed)

It can provide a false code-coverage completion, based on line execution, and not by statement execution.

Even if I separate statements in multiline, the code will be ignored as a not covered code, even if it was executed.

-- BASE SOLUTION --

I thinking about capture cursor offset when execute statement, and not by line. It'll be great if xdebug_start_code_coverage() have a new option to detail coverage.

Example result:

Line 0: <?php $number = isset($number) ? $number : 123; ?>
Line 1: <?php hello(); ?>

$coverage = [
// int line,
// int offset,
// int length,
// enum status { -1: dead code, 0: need to cover, 1: covered }

// If one of statements was not executed
// then it will results in detailed code coverage (until you cover it).
[ 0, 16, 14, 1 ], // isset($number)
[ 0, 33, 7,  1 ], // $number
[ 0, 43, 3,  0 ], // 123

// If entire line was covered, so set offset and length to 0.
// It should sounds like: entire line covered.
[ 1, 0, 0, 1 ],   // entire Line 1

];

After I fix the Line 0 else statement (covering it), results should be:

$coverage = [
[ 0, 0, 0, 1 ], // entire Line 0
[ 1, 0, 0, 1 ], // entire Line 1
];

Additional Information

I attached four images.

  • example1: shows that all line was marked as covered, but only the true conditional was really executed.
  • example2: same example, but separated in multiline. Even the third line not really executed, it shows as executed because it's part of general statement.
  • example3: if conditional. It marks as ignored coverage for next statement. In this case, it should be like valid but not covered because first expression is true.
  • example4: if conditional that will fail on first and validate on second expression. Even, it'll shows second expression as ignored.
TagsNo tags attached.
Attached Files
xdebug-images.zip (16,362 bytes)
Operating System
PHP Version5.6.15-5.6.19

Activities

derick

2015-12-08 15:41

administrator   ~0003299

Hi!

Xdebug already does something like this - it's the branch/path coverage functionality that's new in Xdebug 2.3. Read about it at http://derickrethans.nl/path-branch-coverage.html — is that doing what you want?

cheers,
Derick

david.proweb

2015-12-08 16:02

reporter   ~0003300

Yeah, seems like that. The problem in my case is the support on PHP_CodeCoverage. Thanks!

derick

2015-12-08 16:13

administrator   ~0003301

That's being worked on too: https://github.com/sebastianbergmann/php-code-coverage/pull/400

Closing this out now. Have fun! :-)

Issue History

Date Modified Username Field Change
2015-12-08 14:50 david.proweb New Issue
2015-12-08 14:50 david.proweb File Added: xdebug-images.zip
2015-12-08 15:41 derick Note Added: 0003299
2015-12-08 15:41 derick Assigned To => derick
2015-12-08 15:41 derick Status new => feedback
2015-12-08 16:02 david.proweb Note Added: 0003300
2015-12-08 16:02 david.proweb Status feedback => assigned
2015-12-08 16:13 derick Note Added: 0003301
2015-12-08 16:13 derick Status assigned => resolved
2015-12-08 16:13 derick Resolution open => no change required
2020-03-12 17:35 derick Category Feature/Change request => Code Coverage