Code coverage and the foreach loop
In our support forums for Visual Studio Team System I’ve seen a large number of questions from customers who have seen inconsistencies from the code coverage tool when covering sections of code that containing a foreach loop. What customers are seeing is that they are getting partially covered blocks when they would expect all of the blocks to be totally covered.
What makes this situation so difficult is the fact that there *are* some blocks that are uncovered in specific foreach loop conditions. Under the surface a foreach loop is more complicated then a for loop or a while loop. For a foreach loop the CLR creates some code that may not be exercised by the loop under specific conditions.
The long and short of this is that when you are testing code coverage with foreach loops, you may see some uncovered or partially covered blocks where you would not expect them. Don’t worry, the coverage tool is not lying to you; there really are uncovered blocks in your code. Unfortunately there is no real workaround for this, as the blocks simply are not being covered and will be reported as such by the code coverage tool. I’m going to research this issue a little more and I’ll report back here if I can find out more about what situations with the foreach loop cause these uncovered blocks.
Comments
- Anonymous
March 06, 2006
The comment has been removed - Anonymous
April 18, 2008
PingBack from http://coverage.bobtheblog.info/?p=809