Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / DevOps / unit-testing

The Code Digger

4.75/5 (3 votes)
7 Sep 2013CPOL3 min read 36.6K  
Code digger analyzes all the possible execution paths of your code and defines the behavior in terms of inputs and outputs.

FYI: - The code digger feature currently only works with portable class libraries. In case you are new to portable class libraries please see this youtube video for quick introduction What are portable class library?

Image 1

Recently I was writing a unit test case for a simple "Maths" function and I missed some important test paths. So when I ran my code coverage I found that 41% part of the code paths where not covered. In case you are new to code coverage read here -> What is code coverage?

Image 2

Now one of the ways to approach this problem is go through the code, analyze it, add some test cases and keep iterating until you are satisfied with test coverage.

But it would be great if we can have some kind of tool which can run through the code and give you the following:

 

  • Number of execution paths of the code.
  • For each path what kind of inputs are there?.
  • What kind of output’s are expected from each of the paths?

In other words some kind of input and output table for the code.

For instance below is a simple function called as "Calculate". The below code has 4 different execution paths.

public int Calculate()

{

if (Operation.Length == 0)

{

throw new Exception(" Operation not sepecified");

}

if (Operation == "+")

{

return Num1 + Num2;

}

else if (Operation == "-")

{

return Num1 - Num2;

}

else
{
return Num2 * Num1;

}

return 0;
}

You can create an input and output table for each of the 4 paths as shown below:

  Input Output
Path 1 Operation.Length == 0 Exception thrown
Path 2 Operation == "+" Num1 + Num2
Path 3 Operation == "-" Num1 – Num2
Path 4 Any other inputs Num1 * Num2

Now this input / output table gives me a road map of what kind of test cases I need to write to achieve a better code coverage.

This where code digger tool came to my help. You need to download code digger from http://visualstudiogallery.msdn.microsoft.com/fb5badda-4ea3-4314-a723-a1975cbdabb4.

Once you have download the setup , you need to just right click on the method and say generate Inputs / outputs table as shown in the below figure.

Image 3

FYI: The code digger feature currently only works with portable class libraries. In case you are new to portable class libraries please see this youtube video for quick introduction What are portable class library ?.

The code digger analyzes all the possible execution paths of your code and defines the behavior in terms of inputs and outputs. While the tool analyzes the code he also uncovers if there are any defects.

If you see the output he has come out with 5 different paths. The fourth paths as we discussed in the previous table and he has added one more path where the "Operation" is set null. This path will throw an exception of NULL . In other words he has also pointed to defects which can arise from the code.

Image 4

Now the derivation from the above input and output table can be as follows:

  • The code has five different paths i.e. we can have five different test cases. Or I will say we can write five different test cases to ensure we have proper code coverage.
    • Operation == null
    • Operation = ""
    • Operation = "+"
    • Operation = "-"
    • Operation = "\0" means anything else.
  • The operation == null is an exception and is not handled in the application. So code digger has also pointed out defects which can arise from the application.

So next time you want to write a great unit test , run once the code digger to give you a road map.

For further reading do watch the below interview preparation videos and step by step video series.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)