To determine what percentage of your project's code is actually being tested by your unit tests, you can use the code coverage feature of Visual Studio. This video shows how code coverage analysis is integrated into the Test Explorer tool.
- [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.
Skill Level Intermediate
Q: Why can't I earn a Certificate of Completion for this course?
A: We publish a new tutorial or tutorials for this course on a regular basis. We are unable to offer a Certificate of Completion because it is an ever-evolving course that is not designed to be completed. Check back often for new movies.