Simple Unit Test Code Coverage Sample

Vahid Farahmandian
4 min readJul 10, 2021

--

Test coverage is a measure used to describe the degree to which the source code of a program is executed when a particular test suite runs. A program with high test coverage, measured as a percentage, has had more of its source code executed during testing, which suggests it has a lower chance of containing undetected software bugs compared to a program with low test coverage[1,2].

Source Code: Simple_CodeCoverage_Sample on GitHub

In previous story I have talked about unit testing by giving a simple sample(Lets create a simple TDD sample). In this story with maintaining the same approach, I am going to show you a simple sample of unit test code coverage.

Suppose that I have a large method and I need to be sure about my code’s quality. How can I acquire this assurance? Writing unit tests will help in this journey, but they are not enough. Why? Because even with unit tests, I am not sure that all of my lines are executed during the unit test’s execution.

The number of lines, which are being tested during the unit test’s execution, is called Unit Test Code Coverage, which is measured as a percentage. For instance, lets take a look at the following sample:[3]

In this code I am going to calculate the power of a number and I have written the following unit test for it:

If I run this test, it will be passed, but do I know which lines of my method are has been tested? Can I be sure that my method will return a correct result for any decimal input? Of course No! Why? Because clearly I have not write a test which tests my code for positive exponent. Also I have not delivered a mechanism to control the very large input values, which can make my code being crashed.

In order to see the covered and uncovered lines, there is many tools available which you can read more about the tools here: (Use code coverage for unit testing| Microsoft Docs) and also I believe that ReSharper’s dotCover is really great tool too: (A Code Coverage Tool for .NET by JetBrains). Using tools introduced in these links you can have a great infrastructure for code coverage, but in this story I like to keep my journey as simple as possible.

In this simple sample I am not going to even use them. Instead, lets simply follow these steps:

right click on unit test method>select Live Unit Testing>and select Include…

You can read more about Live Unit Testing here: (Live Unit Testing|Microsoft Docs). Please be aware that by enabling the live unit testing, whenever you modify and save your code, the unit tests will be executed automatically, which might have negative impact on your system’s performance.

By clicking on Include… you will see some green tick, red cross and hyphen icons beside your code lines. Clearly green tick icons, try to tell you that the line is covered by at least one unit test without any failure whereas the red cross tells the same thing but points the failure in unit test and finally hyphens try to tell you that line is not covered by any unit test.

As we can see, the “else” part of my code does not covered by any unit test, and if you pay attention to this part, you can discover a bug in calculation! So lets go ahead by writing one other unit test and cover the uncovered parts:

Ooo my Gosh! Red Crosses…

Now every lines of my code are being covered by the unit tests, however there is a problem in one of my unit tests and its related part of code! lets go and solve the problem…

Hmm now my unit tests are passing and also every lines of my code are covered by unit tests too! Now I have more confident about the code and it might be less error prone.

Finally here is the final code, which I have restricted the input values:

As you see writing the unit test and analyzing it’s coverage is not that difficult, but it could be really useful.

Source Code: Simple_CodeCoverage_Sample on GitHub

References:

  1. Preface | Microsoft Docs
  2. Laurie Williams (software engineer) — Wikipedia
  3. https://stackoverflow.com/a/58110671/1666800

--

--

Vahid Farahmandian

Mainly a .Net #developer, sometimes #SQLServer admin, not yet a husband and specifically not a father. My LinkedIn: https://www.linkedin.com/in/vfarahmandian/