Showing posts with label Frameworks.. Show all posts
Showing posts with label Frameworks.. Show all posts

Sunday, 15 March 2009

NUnit Extensions - Adding Logic at Run Start

Most of the time using NUnit is very straight forward. Recently however I needed to add some functionality that will make life a little bit easier. In this post I'll show how to add some setup code that will be executed once at the start of the test run.

Why is this useful? actually I don't have a clear answer. Here are some reasons I came up with :

  1. Run some special set up code which is just to costly to run at every setup (even if only for class setup).
  2. Transparently enforce some logic on all tests (i.e. without the need for other programmer to explicitly add that logic)
  3. you want to add some special behavior to the test runner

 

NUnit Event Listeners

NUnit has several extension points that will adding almost any kind of logic to the framework. Ben Hall has posted some time back a great article describing the various possibilities For my needs (which are described here) I have been using the Event Listener extension point. Here's the class implementing the event listener:

[NUnitAddinAttribute(Type=ExtensionType.Core,Name = "My Add In",Description="A Demo for Run Setup Code")]
public class CTestingAddin : IAddin, EventListener
{
#region IAddin Members

public bool Install(IExtensionHost host)
{
IExtensionPoint listeners = host.GetExtensionPoint("EventListeners");
if (listeners == null)
return false;

listeners.Install(this);
return true;
}

#endregion

#region
EventListener Members

public void RunStarted(
           string name, int testCount)
    {
//Do Set up logic Here
    }
    public void RunFinished(Exception exception)
{ }

public void RunFinished(TestResult result)
{ }

public void SuiteFinished(TestSuiteResult result)
{ }

public void SuiteStarted(TestName testName)
{ }

public void TestFinished(TestCaseResult result)
{ }

public void TestOutput(TestOutput testOutput)
{ }

public void TestStarted(TestName testName)
{ }

public void UnhandledException(Exception exception)
{ }

#endregion
}

Deployment and execution

That's all the code needed and it can be wrapped as a simple class library (dll). The dll created will need to reference the nunit.core and nunit.core.interfaces. To deploy just copy the dll to the NUnit bin\addin directory. To verify it was properly deployed open the NUnit GUI and check the tools menu under add-in to see that the add-in is there.

I will finish with two tips that will make life easier to run and debug the code.

  1. Its worth while to add a post build event that will deploy the add-in in into NUnit directory at the end of a successful build. In order to do that open the add-in project property and the following to Post build Event Command Line: copy $(TargetPath) <NUnit Directory>\bin\addins.
  2. A convenient way to debug the add-in is to put the NUnit GUI as the "Start action" of the Add-in project. This will enable putting break points normally in the code. (another option is to use the Attach To Process mechanism Ben has suggested).

Sunday, 1 March 2009

Sela Open House

Last week I gave at Sela a lecture about Design For testability.  For all those who attended I want to thank you for coming.

For all those who missed it, you might be able to catch it this week (I'll be giving it again at Sela on Tuesday) or if you happen to be located at the north parts of Israel it will be repeated nearer to you on the 25/3.

For everyone else you can get the slides here.

Tuesday, 16 December 2008

New Mocking Frameworks (C++ and Java)

Over the last couple of months (maybe half a year) there's a big discussion whether TDD/AUT is here to stay or is it just a passing fashion.

I cant predict the future and as much as I believe in these concepts, only time will tell. However, since I'm paranoid by nature, I'm always on the lookout for indications that TDD is here to stay. A strong indication for me is the creation of new tools to help and simplify the writing of unit tests.

I don't know if this is a pure incident or if someone up there is orchestrating all these but during the last week I was introduced not to one but to two new tools out there. Both of them are new mocking frameworks.

The first one is a a mocking framework for C++ released by no other then Google! (named GoogleMock) this framework complete the unit testing framework Google release not too long ago (GoogleTest).

The second framework is a really cool mocking framework released for the Java language - PowerMock. Like JMockit and the .NET counterpart Isolator this framework is based on the principle of Interception (achieved by bytecode manipulation/CLR code injection)  and will allow mocking any (and I mean ANY) kind of method, including Static, Private and Final methods.

For me this has been a productive week for tool developers.

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Walgreens Printable Coupons