Click here to Skip to main content
13,348,514 members (79,360 online)
Click here to Skip to main content
Add your own
alternative version


29 bookmarked
Posted 8 Mar 2005

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.


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)

  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];

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
    [CodePathRange(1, 6)]
    public class ...


  • CodePaths
    [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.


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.


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 projects, MyXaml, a declarative (XML) instantiation engine and the Advanced Unit Testing framework, and Interacx, a commercial n-tier RAD application suite.  Visit his website,, where you will find many of his articles and his blog.

Marc lives in Philmont, NY.

You may also be interested in...


Comments and Discussions

GeneralInteger Expression Pin
Brigl13-Mar-06 7:08
memberBrigl13-Mar-06 7:08 
GeneralCode Coverage Pin
Johan Skårman16-Mar-05 22:59
memberJohan Skårman16-Mar-05 22:59 
I'm pretty sure Code Coverage will be included in Visual Studio 2005. It's in the Beta 2 version.
GeneralNCover Pin
Daniel Turini10-Mar-05 7:02
memberDaniel Turini10-Mar-05 7:02 
GeneralCode Coverage Pin
jmw9-Mar-05 7:18
memberjmw9-Mar-05 7:18 
GeneralRe: Code Coverage Pin
Marc Clifton9-Mar-05 9:24
protectorMarc Clifton9-Mar-05 9:24 
GeneralRe: Code Coverage Pin
jmw9-Mar-05 9:27
memberjmw9-Mar-05 9:27 
GeneralRe: Code Coverage Pin
Marc Clifton9-Mar-05 9:37
protectorMarc Clifton9-Mar-05 9:37 
GeneralRe: Code Coverage Pin
jmw9-Mar-05 9:49
memberjmw9-Mar-05 9:49 
GeneralRe: Code Coverage Pin
dchrno9-Mar-05 10:18
memberdchrno9-Mar-05 10:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin 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
Web03 | 2.8.180111.1 | Last Updated 8 Mar 2005
Article Copyright 2005 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid