Incorrect code coverage results on XCode when testing exceptions Incorrect code coverage results on XCode when testing exceptions xcode xcode

Incorrect code coverage results on XCode when testing exceptions


The line with @throw on it does not complete (because the exception is thrown), so it doesn't get marked as covered. You could file a bug, but this is likely pretty hard for them to fix. If it's a single line in a branch statement, it may be very hard to tell if it was tested, but if there are lines before it that were executed, you'll just have to assume that it was as well.

The bad thing is you'll never be able to get to 100%.


The even worse problem is that it seems the counters of the lines before the @throw in the same condition block are also not coveralbe. Therefore, simpliy write code before @throw as a marker is not going to help the problem.

However, I found that conditions including variables ("if(YES)", "if(1==1)" are not in the cases) are always coverable. Therefore, a tricky thing we could do is to first define a trivial condition variable and then add a condition test including that variable before @throw.

static BOOL __trivialYES = YES;   //for cover @throw, and never use 'const'

then

if(__trivialYES) @throw ...;

This should helps the problem and for your convenient, you may define your own macro to do these things.

#define #throw if (__trivialYES)

and then the throw statement:

#throw ...;

and this probably make the coverage test work better.

PS: the '#throw' is just an example macro. It is the same as other macro. The '#' just a valid charactor (for some precompilers), which make it look special.