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.

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
Tags
No Comments

Please login or register to post comments.