Monday, February 6, 2012

NCrunch, The Incredible TDD Tool!

I've been meaning to evaluate and review some development tools since forever. I think I waited so long just because I never found a tool that grabbed my attention to a level where it could really get me on my butt and make me jot down a review. Well, now we have such an awesomized tool - NCrunch!



Calling NCrunch just another TDD tool would be terrible injustice to it's developers.

NCrunch is -
  • An automated parallel continuous testing tool for Visual Studio.
  • A code coverage analyzer
  • A performance analyzer
  • And last, but not the least, incredibly smart!
Think about the usual steps you go through with test driven development:
  • Write test
  • Build and run tests
  • Write code
  • Build and run tests
  • Refactor code
  • Build and run tests
As a highly optimised test runner, NCrunch can knock half of these steps away by automating them completely - so you end up with:
  • Write test
  • Write code
  • Refactor code
Yes! The tests are automatically built and run in the background. Continuous feedback is provided on the fly, thereby making test development easier. As soon as the tests are build-able, NCrunch runs the tests and provides immediate feedback. Writing assertions is a pleasure!

And there's much more apart from this main feature. But, let's look at how a developer would use NCrunch. Here, we have written a simple test.


The view above shows the normal view that a developer shall have when NCrunch is disabled. Go ahead and enable NCrunch. Go to NCrunch -> Enable NCrunch. And now you see your editor shows something like this. Note that we have defined a default implementation for the Add() method that returns zero.


We shall go back and fix the method Add(). Also, you don't even need to save your files for NCrunch to be able to run your tests!


We now add another test to our suite.


While writing the test however, we see an error. This is because the test is wrong! Correct the test! I just love this level of immediate feedback! Hover over the red cross mark and it shows you a tooltip that tells you exactly what went wrong. (Click on image to view)


Fix the failing test and everything shows green again! NCrunch also handles exceptions gracefully and the tooltips indicate which exception occurred and where.

Code Coverage
The lines of code marked green indicate the parts of code that are covered by tests. This makes it very easy to spot areas that have not been covered yet, write covering code or eliminating any dead code. The following tooltip on the test tells you that the test is covered by 1 passing test.



Performance metrics
NCrunch also provides information about performance metrics and hot-spots in the tested code, thereby allowing you to identify parts of code that might induce a performance problem in the long run. The previous figure shows you the execution time of the test. The next figure shows you that the method being tested is too slow.


The yellow marker indicates that the code is executing really slow and that you should take a look to see if that's normal.

What frameworks does NCrunch support?
Supported testing frameworks are:
  • NUnit
  • MS Test
  • Xunit
  • MbUnit
  • MSpec
Supported languages and .NET versions are:
  • C#
  • VB.NET
  • F#
  • .NET Framework v2.0 and above
  • Visual Studio 2008
  • Visual Studio 2010
  • Visual Studio 11 Developer Preview
Pricing?
I'm sure that there has been a huge level of effort that has gone into developing this tool. But, the software is free to be used as long as it is in beta. Let's hope that on release the developers will price it economically.

There's a lot of other stuff that NCrunch supports that would make your life a wee bit easier. Go check out the NCrunch blog for updates! I really hope that this tool becomes a standard in everyday development for most software development firms.

Have fun with NCrunch!

Links:
NCrunch main website - http://www.ncrunch.net/
NCrunch Download - http://www.ncrunch.net/download.htm
NCrunch on Visual Studio Code Gallery - http://visualstudiogallery.msdn.microsoft.com/6946579c-c7d9-48ce-b039-994d30b85116

Saturday, February 4, 2012

IBM Research | Ponder This - IBM's monthly mathematical puzzles

A few days ago I came across this website by IBM - IBM Research | Ponder This. I'm no big fan of solving insane mathematical puzzles that boggle the mind. But, I really liked the level of the complexity these problems have. It has been going on for quite some time.

I've always wondered what the reason could be for such a huge corporation to run such mathematical puzzles. Maybe they are unable to solve these problems and want help from people. That would be funny. They can easily get other people to solve their problems for practically nothing.

I'm definitely not able to solve any of these problems yet. But, you might - if you're really know your math. Looks like they just posted a new one for February 2012. All the best!

Friday, February 3, 2012

Write to a console window from a Windows application

Ever wondered how you could write out to a separate console window from your Windows application rather than writing to a log file? You might need to do that for a number of reasons – mostly related to debugging. Here’s how you do it:

Predefined APIs are available in kernel32.dll that will allow you to write to a console.

AllocConsole

FreeConsole

AttachConsole

AllocConsole() attaches your process to a console window where the process’ standard output will be redirected. A process can be associated with only one console, so the AllocConsole() function fails if the calling process is already associated with a console. FreeConsole() is used to detach the process from the current console and AttachConsole() can be used to attach associate a console with the current process.

The following shows a sample usage of the feature.
private void button1_Click(object sender, EventArgs e)
{
    new Thread(new ThreadStart(delegate()
    {
        AllocConsole();
        for (uint i = 0; i < 1000000; ++i)
        {
            Console.WriteLine("Hello " + i);
        }

        FreeConsole();
    })).Start();
}

You’ll need to import the AllocConsole and FreeConsole API from kernel32.dll.
[DllImport("kernel32.dll")]
public static extern bool AllocConsole();

[DllImport("kernel32.dll")]
public static extern bool FreeConsole();

And you can always make it Conditional if you want to use it only while debugging.
private void button1_Click(object sender, EventArgs e)
{
    new Thread(new ThreadStart(delegate()
    {
        AllocateConsole();
        for (uint i = 0; i < 1000000; ++i)
        {
            Console.WriteLine("Hello " + i);
        }

        DeallocateConsole();
    })).Start();
}

[Conditional("DEBUG")]
private void AllocateConsole()
{
    AllocConsole();
}

[Conditional("DEBUG")]
private void DeallocateConsole()
{
    FreeConsole();
}