Can't get 100% code coverage

We're attempting to achieve 100% code coverage for a project, and we're having a hard time.  On one particular class, it looks like all code is being covered, but our code coverage report says 12.5% is not being covered.  I looked at the code, and one line is showing as partially covered, and I can't figure out why.  I've included the code below.  The line "if (disposing && (components != null))" is colored blue, and everything else is colored green.  I can't figure out why that one line would be only partially covered.  Both conditions in the "if" are true, and so both should be being evaluated.  What can we do to achieve the elusive 100%

Thanks.

protected override void Dispose(bool disposing)
{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}


 


Answer this question

Can't get 100% code coverage

  • Steve Roszko

    Could be a code-coverage issue - although it might help if you can examine the IL that's generated.
  • Tom Albrecht

     Ernst Kuschke wrote:

    Are you sure both expressions are true The && operator only evaluates the second operand if necessary (if the first operand evaluates to true). In your code:

    if (disposing && (components != null)) { //... }

    If the first operand to && (disposing), evaluates to false, the second operand (components != null) will never be evaluated.

     

    -E

    The original poster said that the only line that wasn't marked as fully covered was that line which means that the lines inside the if were marked as fully covered. If this is true then the if statement would have had to have evaluated as true.... which means it should have full coverage.

    I have also come across this numerous times. The only way to solve it is to put an & instead of an &&... I assume that the code coverage behaviour is like this because not all combinations of the && failing are being tested... In your example above the if could fail if the disposing is false or if components is equal to null... now if the unit tests (or running of the app with code coverage profiling turned on) only checks when disposing is false then the combination of disposing being true and components being false is not checked..... maybe that is why the code coverage is coming up as partial.... either way... it's not clear from the documentation and it's a bit misleading.


  • krissmith

     Kunjal Karamshi wrote:
    This could simply be a case of short-cut evaluation occuring when either one of the conditions is false.


    But if the code inside the if is being executed, which it must be as the original poster said everything else is green, then at some stage the if statement must have evaluated to true, meaning that both sides of the expression evaluated to true.... so if both sides of the expression are being evaluated then shouldn't the code coverage be full for that line instead of partial

  • Joe Szymanski

    This could simply be a case of short-cut evaluation occuring when either one of the conditions is false.
  • Josh Pendergrass

    Are you sure both expressions are true The && operator only evaluates the second operand if necessary (if the first operand evaluates to true). In your code:

    if (disposing && (components != null)) { //... }

    If the first operand to && (disposing), evaluates to false, the second operand (components != null) will never be evaluated.

     

    -E



  • Can't get 100% code coverage