Mark Miller

New in CodeRush 13.2: Unit Test Builder

Here’s a new feature that will help you generate test cases as you’re stepping through code. You know the scenario – you’re debugging and find a problem caused by the data passed in or the state of the software. You might want to continue stepping through the code but you also want to add a test case for the method you’re in right now.

CodeRush has a cool new feature to help out in this situation. The Unit Test Builder (UTB). Here’s how it works:

  1. You will need at least one test project referencing at least one test framework. Which framework you pick doesn’t matter. CodeRush supports them all, and the UTB supports projects with references to multiple test frameworks.
  2. Start your debugging session and get to someplace interesting. For example, here I have a call to a class that calculates prime numbers:


    As you can see from the Expression Explorer, we’re passing in 4 and the IsPrime method is returning true. Four is NOT a prime number, so this is clearly a bug. Let’s step into the IsPrime method…


    This is the method that is returning the incorrect value when the candidate parameter is four. Four is not a prime number, so it should return false instead of true. We can figure out why later, but for now we should add a test case, so we….
  3. Press Ctrl+Alt+Shift+T to generate a new test case for this method, with the data passed in matching our arguments. The Unit Test Builder will appear:

    Here you can see a list of tests that will be generated after the debugging session ends. Hovering over the method in the “Method Called” column produces a hint showing the values passed in:

  4. Let’s rename the test method. Let’s call it FourIsNotPrime.

  5. We can place this method inside a different class if we want, or we can use the existing test class, or we can create a new test fixture to hold our test method.

  6. Finally, we can add optional remarks that will appear inside an XML doc comment.


    Note that all of these steps with the UTB (4-6, above) are completely optional. You can continue to debug and add test methods without making any changes to the names of the test methods or where they will be placed.
  7. Continue to debug, and add more tests as needed.

  8. Finally, when you’re finished, stop the session or close the application you’re debugging just as you normally would. At this point all the tests we’ve added to the UTB will be generated.


    Double-click a test in the UTB to navigate to that test.
  9. Now you add the assertion code (CodeRush has templates for this – “at” yields Assert.IsTrue, and “af” gives you Assert.IsFalse, for example).

The final code for our test fixture looks like this:

public class CalculatorTests
  Calculator _Calc;
  public void Initialize()
    _Calc = new Calculator();
    _Calc.Owner = this;
  public void TestIsPrime5()
    int candidate = 5;
    bool result = _Calc.IsPrime(candidate);
  public void TestIsPrime10()
    int candidate = 10;
    bool result = _Calc.IsPrime(candidate);
  // These two methods were generated following the
  // steps shown above in this blog. Note that the
  // UTB is smart enough to realize that we need
  // an instance of Calculator and that this test 
  // fixture already had one. So we use that instance 
  // in the generated test methods. 
  /// <summary> 
  /// Four is not a prime number! 
  /// </summary> 
  public void FourIsNotPrime()
    int candidate = 4;
    bool result = _Calc.IsPrime(candidate);
  /// <summary> 
  /// Nine is not a prime number! 
  /// </summary> 
  public void NineIsNotPrime()
    int candidate = 9;
    bool result = _Calc.IsPrime(candidate);

The UTB is pretty cool. Give it a try and let us know what you think.

Published Dec 04 2013, 01:47 PM by
Filed under:
Bookmark and Share


Boris Bosnjak

Great work!

December 5, 2013 9:04 AM


That. Is. Amazing! Coderush team really do understand how developper works. :D

December 11, 2013 11:10 AM

About Mark Miller (DevExpress)

Mark Miller is a C# MVP with strong expertise in decoupled design, plug-in architectures, and great UI. Mark is Chief Architect of the IDE Tools division at Developer Express, and is the visionary force behind productivity tools like CodeRush and Refactor!, as well as the DXCore extensibility layer for Visual Studio. Mark is a popular speaker at conferences around the world and has been writing software for over two decades.

Chat is one of the many ways you can contact members of the DevExpress Team.
We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

If you need additional product information, write to us at or call us at +1 (818) 844-3383


DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, Delphi, HTML5 or iOS & Android development. Whether using WPF, ASP.NET, WinForms, HTML5 or Windows 10, DevExpress tools help you build and deliver your best in the shortest time possible.

Copyright © 1998-2018 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners