Click here to Skip to main content
Click here to Skip to main content

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)

Share

About the Author

Shivprasad koirala
Architect http://www.questpond.com
India India

I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small
E-learning company in India. We are very much active in making training videos ,
writing books and corporate trainings. Do visit my site for 
.NET, C# , design pattern , WCF , Silverlight
, LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server  training 
and Interview questions and answers


Comments and Discussions

 
QuestionWouldn't this be better as a blog post or tip PinmvpMika Wendelius7-Sep-13 8:53 
AnswerRe: Wouldn't this be better as a blog post or tip PinmvpShivprasad koirala7-Sep-13 8:55 
GeneralRe: Wouldn't this be better as a blog post or tip PinmvpMika Wendelius7-Sep-13 9:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

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