Code Coverage

Naively, code coverage is the fraction or percentage of code paths executed by some test or test suite of a program. Normally measured by a tool which executes the test and logs the lines of code the test "touches" while running. At its most basic, every conditional statement creates a "branch" defining two unique code paths, and theoretically, both "branches" of each condition must be executed by the test suite in order for the developer to be certain that the code behaves correctly in each condition.

Code coverage is often used as a metric to determine the efficacy of UnitTests. Low coverage typically means that developers are not writing adequate unit tests; there are many code paths in the system which may possibly behave incorrectly, but because no unit test results in execution of that path, the developer (and other stakeholders) can't be confident in the correctness of that code either way. Code coverage can also identify SpeculativeCoding?; code written because it *might* be needed in some hypothetical situation, which the documented requirements (from which the tests were ostensibly developed) do not mention.

The ideal goal is 100% code coverage; the unit test suite executes each and every line of code in the codebase. In most non-trivial programs, this is infeasible:

An important concept to understand is that CoverageIsNotExercise?. The fact that a line of code (or some part of it in the case of compound statements) is being executed does not necessarily mean that the expected result of that execution is properly asserted, nor that all expected permutations of all code paths are executed and assertions made. As a result, code may have passing unit tests with 100% coverage and still be full of defects, because the unit tests do not adequately "exercise" the code to assert it behaves correctly in all expected situations.

(Better descriptions welcomed; meanwhile see (eg) http://www.testingcraft.com/coverage.html.)

See "Introduction to Code Coverage" article by Lasse Koskela at http://www.javaranch.com/newsletter/200401/IntroToCodeCoverage.html

See: CodeCoverageTools, TddCodeCoverage ("What CodeCoverage do you see on TestDrivenDevelopment projects?")


Isn't it natural to use code coverage tools to see how well our Unit Tests have done? Yet today in Smalltalk, Unit Tests are very popular and code coverage seems almost unheard of. There is no equivalent to SUnit. What's going on? Is the current situation one we should be happy with?


Sigh. I've long harbored the suspicion that if you added up all of the things you'd have to do to avoid being "surely unprofessional", you would a) never sleep, and b) never ship anything to anyone other than the federal government. --DaveSmith

I changed the tone of the opening comment.

I think there is something startling and probably sub-optimal about this absence. Many people didn't use unit tests until they were advocated here. Similarly I suspect they don't use code coverage tools through ignorance rather then a conscious decision about what is professional/successful. -- DaveHarris

Not out of ignorance, no. I've long noticed how long it takes testers who are using code coverage tools to get anything tested. I've also noticed how quickly I can get code tested and out there. I've also noticed that their code, despite being so carefully tested, is not higher quality than mine. It could be that coverage analysis would make my tests better. But it could also be that the benefit of coverage analysis would not pay for the cost. I could have spent that time refactoring my code so that it is clean, talking to the customer so I understand the problem, and talking to the other programmers so I understand the system. These things, I think, have a very high payoff. A technique such as code coverage must have a very high payoff indeed to be more worthwhile.

Also, properly factored OOP code tends to be pretty simple and doesn't have that many branches. I won't claim that every line of code I write is like that (don't I wish!), but it turns out to be pretty easy to make tests that adequately exercise all the branches when there aren't that many branches to begin with. No fancy tools needed. -- WayneConrad

Maybe the ignorance is mostly mine.

I have been thinking about branches. To test a switch statement properly we need to ensure that the test data includes every case. If we replace a switch statement with a polymorphic message send, do we now have to ensure that the test data includes an instance of every class?

I think the answer is possibly "no", on the grounds that the message dispatch is managed automatically and presumably flawlessly, so doesn't need to be tested, where-as the switch code is written out by hand. However, it still bothers me a little that we have interactions that are "less tested" in the OO case. This may be a weakness of OO rather than a strength. We are relying on Liskov Substitution instead, so it depends on how confident we are that our classes are truly substitutable. -- DaveHarris

I think that's exactly right. -- WayneConrad

When you test a switch statement, you not only want to test the "manual dispatch" code. You also want to check that the code for every case is correct. Now if you use methods instead, the first part disappears because the compiler does the job for you. You still want to check that every case (that is, implementation of the method) is correct. Therefore you need to ensure that the test data includes enough instances to run code in every overriding of the method. So I don't see why this second version would be "less tested". Am I missing something? -- DanielBonniot


I don't know about professional, but there seems to be an obvious fit between the two tool categories. If they aren't being used together (I don't know that they aren't) it might be because the "obvious" turns out, in practice, not to be true.

Suggestions of coverage tools which integrate well with xUnit, even commercial ones, but preferably with an eval version readily available, would be welcome. I for one am both an enthusiastic JavaUnit user and eager to look into whether code coverage tools might improve the results I get from unit tests, for instance by pointing out portions of my code that don't get tested but where things might break. -- LaurentBossavit


My experience with CodeCoverage tools has been negative (using mostly IBM's PureCoverage?). It seemed mostly like the number was something about which the PointyHairedBoss could pat themselves on the back of it was over 95% and ruthlessly attack the developers if it was under. They didn't know how to read the report, just that one number. They had no clude about getters/setters and why they weren't tested. They didn't know that if a class isn't touched at all during your unit test suite in PureCoverage? it wasn't aggregated into the results (so you could literally get 100% by having no tests).

Overall, the CodeCoverage reports had little impact on whether the developers actually wrote good unit tests that actually covered a good majority of the relevant cases, only CodeReviews? could do that. I'd say don't bother with CodeCoverage, it's not worth your time.


[Descriptions of tools moved to CodeCoverageTools.]


EditText of this page (last edited September 5, 2012) or FindPage with title or text search