{"id":9281,"date":"2007-05-07T09:30:41","date_gmt":"2007-05-07T09:30:41","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/bharry\/2007\/05\/07\/managing-quality-part-7-code-coverage\/"},"modified":"2018-08-14T00:33:24","modified_gmt":"2018-08-14T00:33:24","slug":"managing-quality-part-7-code-coverage","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/bharry\/managing-quality-part-7-code-coverage\/","title":{"rendered":"Managing Quality (part 7) &#8211; Code Coverage"},"content":{"rendered":"<p>Code coverage is a multi-edged sword \ud83d\ude42&nbsp; There is no one right answer for how to do it and there are many ways to misuse it.&nbsp; Here I&#8217;ll talk about how to think about code coverage and then talk about what we do and reports we use to track it.<\/p>\n<p>Let&#8217;s say your test pass rate is 99.9%.&nbsp; Is that good?&nbsp; Well, maybe.&nbsp; It&#8217;s not nearly enough information to know.&nbsp; Are the tests testing the primary user scenarios?&nbsp; Do you have enough tests?&nbsp; Are you missing testing on entire modules?&nbsp; Code coverage is one tool in the toolshed to help answer the question &#8220;Is that good?&#8221;<\/p>\n<p>Code coverage data is collected while your tests are run (both automated and manual) and records what blocks in your code are covered (and not covered).&nbsp; It can be used in many scenarios:<\/p>\n<ul>\n<li>To determine what parts of your code are not getting covered and allow you to make intelligent decisions about whether or not to&nbsp;do additional testing&nbsp;to cover that code.<\/li>\n<li>To determine when you have too many tests &#8211; lots of tests that test basically the same code and don&#8217;t cover distinct scenarios.<\/li>\n<li>Combined with pass rate, defect rate and code churn info, help prioritize where do direct your testing efforts.<\/li>\n<li>Identify &#8220;dead&#8221; code that can never be called so it can be removed.<\/li>\n<\/ul>\n<h3>How much is enough?<\/h3>\n<p>Code coverage is often used as a metric to determine whether sufficient testing has been done to &#8220;release&#8221; software.&nbsp; The age old question is &#8220;How much is enough?&#8221;&nbsp; One size does not fit all. Except in the most extreme cases, 100% is not the right answer.&nbsp; Think about what would be involved in 100%.&nbsp; You&#8217;d have to write tests to execute every single exception handler, every single branch in your code.&nbsp; Your code probably includes machine generated code &#8211; for example, web service SOAP proxies that you don&#8217;t even use.&nbsp; There are other example of &#8220;dead&#8221; code that is hard or inadvisable to remove.<\/p>\n<p>So, if it&#8217;s not 100%, what is it?&nbsp; We use a minimum bar of 70%, with the vast majority of that coming from automated tests.&nbsp; We only use manual testing coverage to bridge gaps in our automated testing.&nbsp; Would more be better?&nbsp; Maybe.&nbsp; One thing to keep in mind is that every automated test you have comes with a tax &#8211; you have to write it, run it and maintain it indefinitely (but this is the subject of a whole different blog post &#8211; so I&#8217;ll try to get to that one in the next few months).<\/p>\n<p>Be careful how you use code coverage.&nbsp; No amount of code coverage will tell you if you have a well tested app.&nbsp; This is because there are tons of bugs that can&#8217;t be flagged by code coverage.&nbsp; The problem in these cases is not that the code isn&#8217;t being covered but rather that the data variation isn&#8217;t sufficient.&nbsp; Buffer overruns are a very simple example of bugs that can still exist in code that has 100% code coverage.&nbsp; This fact really hit me most as we were building TFS and I realized how susceptible SQL is to this problem.&nbsp; Running every query\/stored procedure tells so little about the correctness of your SQL code.&nbsp; SQL depends so much on the data you have in the database and the values you query for.<\/p>\n<p>I&#8217;ve seen teams drive for exceptionally high code coverage numbers.&nbsp; Like everything else in this world, the closer you want to get to &#8220;perfect&#8221;, the harder it is.&nbsp; It&#8217;s much easier to go from 20%-&gt;30% than from 60%-&gt;70% and even harder to go from 80%-&gt;90%.&nbsp; Grinding out test after test to cover every block of code becomes an exercise of increasing effort and decreasing return (in terms of useful bugs found).&nbsp; At some point you are better off directing that extra effort at alternative testing approaches.&nbsp; For us, we&#8217;ve decided 70% is the min bar.&nbsp; Making that the min bar, of course means that our average is a bit higher than that.<\/p>\n<h3>The DevDiv code coverage methodology<\/h3>\n<p>As I mentioned before, we focus primarily on automated testing code coverage.&nbsp; The reason is that we have to test on such a wide range of configurations, we must be able to rely on our automated tests to find the vast majority of problems.&nbsp; We break things down at the DLL\/Assembly level and our metrics are based around the percentage of component that have 70% or better code coverage.&nbsp; Our primary report looks like this and shows % of components and the % code coverage of that component by coverage band, trended by build:<\/p>\n<p><img decoding=\"async\" height=\"289\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/8\/2019\/02\/clip_image002%5B3%5D.jpg\" width=\"482\"><\/p>\n<p>We classify each componentby % code coverage bands and track the trends over time.&nbsp; By the time we ship Orcas, the bar should be 100% green.&nbsp; Our goal for Beta 1 was 75% of components over 70% code coverage.&nbsp; Our goal for Beta 2 is 90% of components over 70% code coverage and our goal for RTM is 100% of components over 70% code coverage.<\/p>\n<p>We then break that down by feature team and factor in the pass rate on each run.&nbsp; Obviously, it lots of tests are failing, it&#8217;s going to affect your coverage numbers.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/8\/2019\/02\/image%7B0%7D%5B4%5D.png\"><img decoding=\"async\" style=\"border-right: 0px;border-top: 0px;border-left: 0px;border-bottom: 0px\" height=\"190\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/8\/2019\/02\/image%7B0%7D_thumb%5B2%5D.png\" width=\"481\" border=\"0\"><\/a> <\/p>\n<p>And then we enumerate each of the binaries that are failing the 70% min bar and identify action plans for each.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/8\/2019\/02\/image%7B0%7D%5B8%5D.png\"><img decoding=\"async\" style=\"border-right: 0px;border-top: 0px;border-left: 0px;border-bottom: 0px\" height=\"443\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/8\/2019\/02\/image%7B0%7D_thumb%5B4%5D.png\" width=\"787\" border=\"0\"><\/a> <\/p>\n<h3>Summary<\/h3>\n<p>Code coverage is a very useful tool in helping to spot areas of your code that need additional testing.&nbsp; Use it that way.&nbsp; Don&#8217;t be a slave to code coverage.&nbsp; Nothing replaces the value of having a person think through all of the scenarios the software supports and describing an appropriate test plan.&nbsp; The code coverage data is a tool to help you refine your test plan and identify areas you may not be thinking about.<\/p>\n<p>Brian<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Code coverage is a multi-edged sword \ud83d\ude42&nbsp; There is no one right answer for how to do it and there are many ways to misuse it.&nbsp; Here I&#8217;ll talk about how to think about code coverage and then talk about what we do and reports we use to track it. Let&#8217;s say your test pass [&hellip;]<\/p>\n","protected":false},"author":244,"featured_media":14617,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[5],"class_list":["post-9281","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-tfs"],"acf":[],"blog_post_summary":"<p>Code coverage is a multi-edged sword \ud83d\ude42&nbsp; There is no one right answer for how to do it and there are many ways to misuse it.&nbsp; Here I&#8217;ll talk about how to think about code coverage and then talk about what we do and reports we use to track it. Let&#8217;s say your test pass [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/posts\/9281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/users\/244"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/comments?post=9281"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/posts\/9281\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/media\/14617"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/media?parent=9281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/categories?post=9281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/bharry\/wp-json\/wp\/v2\/tags?post=9281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}