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

Simple Code Path Testing

, 8 Mar 2005
Rate this:
Please Sign up or sign in to vote.
Add code path testing to your unit tests.

Introduction

I'm working on a core engine (something that is completely divorced from the Windows.System.Forms namespace but offers extensibility) for MyXaml, and the property setter is a complex beast with lots of different code paths. Besides wanting to make sure that all the code paths are tested, I also want to find out what code paths are never executed, usually due to too many patches and fixes applied, resulting in some brain dead code.

So, I got to thinking, how would I test my code paths? Something simple that doesn't require parsing out the IL or other highly complex techniques. Then I thought, why not use a custom Trace listener, put a specific Trace.Write() statement at the start of each path, and have the unit test engine tell me what code paths didn't execute after running all the tests in the fixture.

So, that's what I did, using my Advanced Unit Test tool. It's a simple solution, requires only a little overhead on the programmer's part to put in the Trace.Write statements, and results in useful information.

Using A Trace Listener

Instead of using a specific string format, I chose to pass a CodePath instance in the Trace.Write(object) method. It's easy to test if the object is a CodePath instance, and by using a class, it can be extended for some yet unknown future functionality. To make life simple, each code path is designated with a unique number. At the start of a method or a branch, simply add something like:

Trace.Write(new CodePath(1));

Each method or branch gets a unique value. This isn't great, as you can accidentally use the same number, but in practice I've found that using a section, like "100-199" for a particular method, helps to keep things organized.

The trace listener implementation is straightforward:

public class CodePathListener : DefaultTraceListener
{
  protected Hashtable codePathTracker;

  public CodePathListener(Hashtable codePathTracker)
  {
    this.codePathTracker=codePathTracker;
  }

  public override void Write(object obj)
  {
    CodePath cp=obj as CodePath;
    if (cp != null)
    {
      if (codePathTracker.Contains(cp.PathNum))
      {
        int n=(int)codePathTracker[cp.PathNum];
        ++n;
        codePathTracker[cp.PathNum]=n;
      }
      else
      {
        codePathTracker[cp.PathNum]=1;
      }
    }
    else
    {
      base.Write(obj);
    }
  }
}

Using a HashTable, where the code path ID is the key, I can also count how many times the code path has executed for the tests in the fixture. However, this information isn't reported in the AUT GUI at this point.

New Attributes In AUT

There are three new attributes in AUT. Two of them can be applied to the test fixture itself:

  • CodePathRange
    [TestFixture]
    [CodePathRange(1, 6)]
    public class ...

    and

  • CodePaths
    [TestFixture]
    [CodePaths(1, 3, 6)]
    public class ...

    The meaning should be obvious--CodePathRange lets you specify an inclusive range of code path IDs that should be encountered by the tests in the fixture, while CodePaths lets you specify individual paths. More sophisticated parsing, like "1-5, 8, 9, 10-19" would be cool, but not implemented.

    A third attribute can be applied directly to the test:

  • ShouldExecuteCodePath
    [Test, ShouldExecuteCodePath(6)]
    public void ...

After the test runs, AUT checks to see, if the test encountered the code path of the supplied ID. The test fails if the code path was not encountered. Only one code path can be tested here, so this would usually be the terminal branch of a method.

Conclusion

That's it! I think it's quite simple but very useful. The download includes a code path test example and the current version of AUT.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Marc Clifton

United States United States
Marc is the creator of two open source projets, MyXaml, a declarative (XML) instantiation engine and the Advanced Unit Testing framework, and Interacx, a commercial n-tier RAD application suite.  Visit his website, www.marcclifton.com, where you will find many of his articles and his blog.
 
Marc lives in Philmont, NY.

Comments and Discussions

 
GeneralInteger Expression PinmemberBrigl13-Mar-06 6:08 
GeneralCode Coverage PinmemberJohan Skårman16-Mar-05 21:59 
GeneralNCover PinmemberDaniel Turini10-Mar-05 6:02 
GeneralCode Coverage Pinmemberjmw9-Mar-05 6:18 
GeneralRe: Code Coverage PinprotectorMarc Clifton9-Mar-05 8:24 
jmw wrote:
you could use nCover
 
Thanks for the info! I'd forgotten that I'd encountered this before. Do you use it? I took a new look, and frankly, I wish there was some "quick start" documentation somewhere showing how to use it. Do you have any links?
 
Marc
 
MyXaml
Advanced Unit Testing
YAPO
 

GeneralRe: Code Coverage Pinmemberjmw9-Mar-05 8:27 
GeneralRe: Code Coverage PinprotectorMarc Clifton9-Mar-05 8:37 
GeneralRe: Code Coverage Pinmemberjmw9-Mar-05 8:49 
GeneralRe: Code Coverage Pinmemberdchrno9-Mar-05 9:18 

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 | Mobile
Web03 | 2.8.140721.1 | Last Updated 8 Mar 2005
Article Copyright 2005 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid