From the course: Visual Studio Developer Tips
Discover untested code with code coverage - Visual Studio Tutorial
From the course: Visual Studio Developer Tips
Discover untested code with code coverage
- [Instructor] I have recorded a lot of tips in my other courses that I think you'll find useful. This tip is from my Visual Studio Essentials Series. You'll find it in the unit test course. I thought I'd revisit this tip on how to discover untested code with code coverage. Before showing the tip, I have some housekeeping notes. First, this feature is only available in the Enterprise edition. Also, the source code for this video is available in two locations. You can download the zip from the original course, or find the sample in the weekly tips GIT repository. I'm using tip number 33. You might notice that the solution name is different in the original video and here in the GIT repository there's also additional solution level folder, but that doesn't impact the example. Enjoy this weeks tip. To determine what percentage of your project's code is actually being tested by your unit test, you can use the code coverage feature of Visual Studio. The more code that is tested in the application, the more thorough your test rests can be. The code coverage features are available in Visual Studio Enterprise edition only. In my project, I have six unit tests. In a real application I might have hundreds or thousands of unit test, for this demonstration I'm using a small subset of tests. I'm looking at my tests and they're all passing. I'm feeling pretty good about our project until a coworker, who walks into my office one day and says, "Did you realize we're not testing "our account class?" I could look through the thousands of unit tests in our project to verify that's true, or I could use an automation tool to look at the code coverage path. In Visual Studio, to use the code coverage tool, you right click on a test in the Test Explorer and then choose Analyze Code Coverage for Selected Test. This would look at the code paths for this one unit test. Typically, you run code coverage on the entire project to verify that all your code paths are being run. If I got up here to the top of this list, and when I run it here, it'll cover all the tests. After a few seconds, a window pops up on the screen. I'll pin it to the bottom of the window and I'll unpin the Test Explorer so we have more room. This shows that the most recent test coverage. Visual Studio caches the previous coverages, so I can see them listed in this drop down. This shows that I have a 62% coverage rate and a 37% uncovered rate. I can drill down into this hierarchy to learn more. So I can see that my brokerage live test is 91% covered. I expected that to be 100%, so what's going on there? Why is some of my unit test code not being run? Well the good news is this class has 100% coverage, so the problem is in this class here. So I'll drill down further, and I'll look at this method I see this is one of the problem methods. So I can double click on this method and then I'll hide this window. What you'll see in Visual Studio, is it moved me over to my test method and it's also showing me the code. The code that's highlighted in blue is the code that run the last test. If you don't see the blue and red, you go to the code coverage result and you click on this button here, Show Code Coverage Color. So I can see that all the code ran except for line 70 and line 71. Let's think about what's happening here. I set Assert.AreEqual(expectedCommission and calculatedComission. My unit test is testing when an exception is thrown. So when this method is run and the exception is thrown, none of the rest of the code in this method is processed. Which means that having this Assert here is useless because the code will never run. Really, the assertion is not here in line 70, the assertion is here in this expected exception. So I can delete this safely. And I can also delete the same line of code in the other method. Now it's telling me that this end of the method didn't run either. That's because we didn't exit the method in the normal fashion. We exited it through an exception. So it's also saying this red line here says we didn't exit the method in the normal fashion. I can't do anything to fix that. So those will show up in the rest of my code coverage tests. This looks good, I'll rebuild my project and see if I got a higher percentage of test coverage. Last time it was around 78%, this time it's running around 92%. Now we'll get on to the original problem which is what's happening in our system under test. The good news is that my payment system class, or any of the classes that are in that namespace are 100% code coverage. In this other namespace, I see that my coworker was correct. I have no unit tests on the account class. So I'll have to go in and add some unit tests there. While I'm looking at this, I also notice that my commission calculator only has 81% coverage rate. So there's something going on in there too. Let's see what that is. I double clicked on the item in the code coverage results screen and it took me to the method and I can see that some of my code is blue meaning it ran, some of it's red, meaning it didn't run and some of it's in yellow, which means that part of the code ran and part of the code didn't run. So what I'm seeing here, is that I have an if statement and because none of the conditions were true in the if statement, it only ran the else clause. So I need a unit test that either checks for gross sale as greater than the sales amount or unit sold is greater than the commission threshold unit amount. I'll need to add another unit test. Keep this number in mind 81% and I'll go over here to my test class. I'll copy this method. Change the name to return top commission when amounts are above thresholds. Now I need to change this unit sold amount to be the number higher than my threshold. That's over here in the commission constants, so it has to be a number higher than 400. So I can either use 401 or another technique I sometimes use is to take the constant value and use that in the unit test, that way if we change the constant value in the future, it will update the unit test. I also need to change this line of code here where the expected commission, it should be not a standard commission rate but it should be the top commission rate. That should be the only two changes I need the make. Open up Test Explorer. Build a Process. Run my unit tests. Everything's green, now look in my code coverage. Now I'm at 100% coder coverage in Commission Calculator. All I need to do now is go in and fix up the account class. That's all I have for this demonstration. I find the Code Coverage tool to be invaluable for verifying my code is thoroughly tested. If you don't have Visual Studio Enterprise, you can't use this feature, but there are third party tools that perform similar tests.
Contents
-
-
-
Brace matching in C#4m 57s
-
(Locked)
Select vertical sections of code with Box Selection5m 50s
-
(Locked)
Move code with keyboard shortcuts2m 49s
-
(Locked)
Find duplicate code with the code clone analysis tool4m 8s
-
(Locked)
Paste JSON/XML as class3m 42s
-
(Locked)
Colorize output in the build window4m 37s
-
(Locked)
A better way to redock a tool window3m 57s
-
(Locked)
The Open Command Line extension3m 39s
-
Get consistent editor settings with EditorConfig3m 46s
-
(Locked)
Git at a glance: Updates to the Visual Studio status bar4m 1s
-
(Locked)
Surround a block of code with a code snippet2m 5s
-
(Locked)
Understand the document tab features3m 35s
-
(Locked)
Clipping to a border using an opacity mask5m 53s
-
(Locked)
Insert Line with Ctrl Enter2m 21s
-
(Locked)
Use the File Tab channel3m 31s
-
(Locked)
Use the Add New File extension4m 27s
-
(Locked)
Keyboard access for CodeLens4m 32s
-
(Locked)
Use the Clipboard Ring2m 56s
-
(Locked)
Use the GlyphFriend extension5m 41s
-
(Locked)
Activate Open File in Solution Explorer3m 14s
-
(Locked)
Use the enhanced scrollbar41s
-
(Locked)
Drag and drop in Solution Explorer5m 20s
-
(Locked)
Keyboard shortcuts for MVC views and controllers4m 17s
-
(Locked)
Pin data tips in debug window6m 6s
-
(Locked)
The Trailing Whitespace Visualizer extension4m 19s
-
(Locked)
Date rounding and other helpers5m 21s
-
(Locked)
Memorize window layout (Reuse)4m 26s
-
Use DialogResult6m 48s
-
(Locked)
Label a breakpoint2m 31s
-
(Locked)
The Image Optimizer extension2m 35s
-
(Locked)
Use Ctrl+Spacebar to fix case of variable names3m 40s
-
(Locked)
Use the nameof operator6m 21s
-
Discover untested code with code coverage7m 36s
-
(Locked)
Use the Hand tool in XAML designer2m 16s
-
(Locked)
Array Visualizer extension2m 35s
-
(Locked)
Use Ctrl+Tab to cycle through windows3m 18s
-
(Locked)
Understand line stroke patterns4m 37s
-
(Locked)
The Comment Remover extension3m 41s
-
(Locked)
Toolbox tip shortcuts6m 1s
-
(Locked)
Randomize data with LINQ4m 13s
-
Customize context menu4m
-
(Locked)
Get the file path1m 25s
-
(Locked)
Convert lists4m 34s
-
(Locked)
Enum flags5m 19s
-
(Locked)
Reference highlighting2m 4s
-
(Locked)
Evaluate performance with PerfTips4m 50s
-
(Locked)
Null propagation4m
-
(Locked)
Multiple editor views of same file2m 17s
-
(Locked)
Customize the output window7m 37s
-
(Locked)
Colored braces with the Viasfora extension4m 15s
-
(Locked)
Use the C# interactive window10m 47s
-
(Locked)
The Smart Semicolon extension3m 29s
-
(Locked)
Reference Highlighting1m 35s
-
(Locked)
IEnumerable Extension Methods4m 58s
-
Compare LiNQ sequences6m
-
(Locked)
The Clear Recent Project List extension3m 34s
-