12,955,170 members (59,999 online)
Tip/Trick
Add your own
alternative version

#### Stats

21.2K views
8 bookmarked
Posted 7 Sep 2013

# The Code Digger

, 7 Sep 2013 CPOL
 Rate this:
Please Sign up or sign in to vote.
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?

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?

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.

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.

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.

## License

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

## About the Author

 Architect http://www.questpond.com India

 Pro

## Comments and Discussions

 First Prev Next
 Wouldn't this be better as a blog post or tip Mika Wendelius7-Sep-13 7:53 Mika Wendelius 7-Sep-13 7:53
 Re: Wouldn't this be better as a blog post or tip Shivprasad koirala7-Sep-13 7:55 Shivprasad koirala 7-Sep-13 7:55
 Re: Wouldn't this be better as a blog post or tip Mika Wendelius7-Sep-13 8:03 Mika Wendelius 7-Sep-13 8:03
 Last Visit: 31-Dec-99 18:00     Last Update: 28-May-17 22:27 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 7 Sep 2013
Article Copyright 2013 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid