basti
basti

Reputation: 41

How to reach full CodeCoverage? Free Blocks through Exception

I've got a little Problem.(at unittesting) - I unittest a class where methods raises exceptions. The structure is:

    public bool DoA()
    {
        ThrowException();
        return true;
    }

    public void DoB()
    {
        ThrowException();
    }

    private static void ThrowException()
    {
        throw new NotSupportedException();
    }

The result from the CodeCoverage is not 100% - the return statement and the closing curly bracket from DoA() and the closing curly bracket from Do()B are not under the codeCoverage(because they not reached). I know, thats not really important for the unittest because i still checked the functionality but just for me - its possible and how i reach the full CodeCoverage? Maybe through exclude? (possible change the testcode / program code)

Upvotes: 2

Views: 971

Answers (2)

Peter
Peter

Reputation: 27944

You can't reach the return true part of you code, so you can remove it. It is bad to have unreachable code in your projects. Or because the function DoA() is not finished, you can use: [ExcludeFromCodeCoverage]

[ExcludeFromCodeCoverage]
public bool DoA()
{
    ThrowException();
    return true;
}

Upvotes: 6

derabbink
derabbink

Reputation: 2429

It's impossible to get full coverage here. Everything after a throw is dead code.

Also, code (line) coverage is not the holy grail in testing. Yes, it is important, but it is not the one ultimate criterion.

Upvotes: 5

Related Questions