Feeds:
Posts
Comments

Archive for October, 2011

Some days ago I read  Jon Skeet‘s twitter post on NCrunch and thought I would give it a try.

The idea of running unit tests right from the IDE without having to call NUnit or any other third party testing framework seems interesting.

To test NCrunch, I reused my GIS solution that has some unit tests.

  • Opened my solution in VS 201o
  • Enabled NCrunch from VS 2010 main menu
  • NCruch wizard popped up

At this point, I had to go back to the code as I needed to do some modifications before running the wizard. Unfortunately that was not possible as the wizard is a modal dialog so I had to cancel it.

Once my modifications on the code done, I tried to go back and open the wizard again by disabling and then enabling NCrunch.

That did not have any effect and I could not reopen the wizard.

As I could not get the wizard back for my GIS solution, I decided to go for a new project from scratch.

Enabling NCrunch for a new project does not work either, not sure but I was expecting the wizard to open for every new solution created within VS2010.

Anyway, the tools looks very simple and easy to configure… trying without the wizard!

I created a test project that contains a simple class that does some math operations, the example below is a method that adds up two integer numbers and returns the result.

public class MathOperations {
   public static int Add(int i, int j) {
       return i+j;
   }
}
[TestFixture]
class MathOperationsTest {
    [Test]
    public void TestAddition() {
        Assert.AreEqual(2, MathOperations.Add(1, 1));
    }
}

Note that if you want to have your tests automatically executed , you need to “enable automatic test execution” from the main NCrunch menu.

NCrunch shows you the test results straight in the IDE which is awesome! No need to go anywhere else to see your results as they will be there sitting right in front of you along with the production code!

The tool uses the standard colors to highlight test results, all successfully tested lines are marked in green, and that happens in real time!

Up to this point, the tool looks great, so far so good! Time to push it a bit into the limits…

For this purpose, the addition method showed above is rewritten using recursion, which involves a more complex execution path and more resources.

   public static int Add(int i, int j) {
      if (j == 0) return i;
      return Add(i+1, j-1);
   }
   [Test]
   public void TestAddition() {
      Assert.AreEqual(300, MathOperations.Add(100, 200));
   }

The resulting output is a bench of green dots, meaning that the production code is doing what it is supposed to do.

Changing the unit test to the following :

   [Test]
   public void TestAddition() {
      Assert.AreEqual(2, MathOperations.Add(1000000, 500000));
   }

Gives… A crash 😦

In this situation, it is preferable for the tool to show a dialog box warning the user of a memory exception rather than crashing.

To wrap up this post, some points to be highlighted:

The good 

  • NCrunch is an excellent tool that helps you test your code without having to leave your IDE. This will save you a couple of clicks and that is something like 5 seconds of your precious time for every test execution you perform
  • There are many other useful features available not covered in this post, check out  NCrunch web site for more details

The bad

  • The tool being in its beta release, there are still some reliability issues that need to be fixed

Still in the process of trying out this tool, I will post any other findings I do while testing my different sized projects.

Happy testing!

Advertisements

Read Full Post »