Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 676
basti Avatar asked Dec 04 '25 10:12

basti


1 Answers

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;
}
like image 142
Peter Avatar answered Dec 07 '25 00:12

Peter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!