Blogs

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:

    SteppingThroughTheCode

    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…

    IsPrime

    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:

    utb1
    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:

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

    utb1rename
  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.

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

    utb1comment

    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.

    utb2
  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.

    utb3

    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:

[TestClass]
public class CalculatorTests
{
  Calculator _Calc;
  [TestInitialize]
  public void Initialize()
  {
    _Calc = new Calculator();
    _Calc.Owner = this;
  }
  [TestMethod]
  public void TestIsPrime5()
  {
    int candidate = 5;
    bool result = _Calc.IsPrime(candidate);
    Assert.IsTrue(result);
  }
  [TestMethod]
  public void TestIsPrime10()
  {
    int candidate = 10;
    bool result = _Calc.IsPrime(candidate);
    Assert.IsFalse(result);
  }
  // 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> 
  [TestMethod]
  public void FourIsNotPrime()
  {
    int candidate = 4;
    bool result = _Calc.IsPrime(candidate);
    Assert.IsFalse(result);
  }
  /// <summary> 
  /// Nine is not a prime number! 
  /// </summary> 
  [TestMethod]
  public void NineIsNotPrime()
  {
    int candidate = 9;
    bool result = _Calc.IsPrime(candidate);
    Assert.IsFalse(result);
  }
}

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

Comments

Boris Bosnjak

Great work!

December 5, 2013 9:04 AM

Crono

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.
LIVE CHAT

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 info@devexpress.com or call us at +1 (818) 844-3383

FOLLOW US

DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, Silverlight, ASP.NET, WinForms, HTML5 or Windows 8, DevExpress tools help you build and deliver your best in the shortest time possible.

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