How do code coverage tools work?

前端 未结 5 1309
小蘑菇
小蘑菇 2021-01-30 08:43

How do code coverage tools like NCover know what parts of the code were executed and what parts were not?

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-30 09:02

    It requires that you run your tests once with code coverage analysis enables, and then simply counts the number of blocks (that is, scope blocks) covered and compares to the total number of blocks in the project(s) you are testing.

    The basic reasoning is that if each possible combination of code blocks is covered, all code paths are covered1. The main argument against putting too much weight in code coverage numbers is that "easy" blocks like getters and setters, that give no real value (and could hardly go wrong...) count just as much as more error-prone blocks of code.


    1) As noted by Ira Baxter in a comment, the previous wording of this sentence was incorrect. Please read the comments for some discussion on this.

提交回复
热议问题