Search code examples
ccode-coverage

C language Unit test code coverage using CTC++ code coverage tool


I am new to c programming. I am doing the C language unit test code coverage using CTC++ code coverage tool and my assert condition is not being covered by the code coverage. What can I do to cover that part as well?


Solution

  • The "problem" with assertions is that they rarely fail. It's often a sanity check: if some condition is false, then the rest of the program will really jump into the woods and it will be very difficult to debug, so better stop it cleanly, even if it's unlikely that it fails here. Example:

    char *x = malloc(100);
    assert(x != NULL);   // how to test so x == NULL ????
    

    So when you're using a coverage tool, the tool has to check both branches of the assert macro, and making it fail is next to impossible with high level tests, and still very difficult with low level tests.

    The perverse effect is that, next time, the programmer won't put as many assert statements as before, just because their failure is untestable.

    The best way is to disable assertions when testing with coverage tool (how to completely disable assertion), so you don't have to justify them (you can even make a general note/waiver in the coverage report about that) and leave them in the program just in case.