Pete Wright's Radio Weblog
Musings on anything and everything, but mainly code!

 

 

18 June 2004
 

Unit testing with nUnit seems to be flavour of the month as far as the press are concerned. Aside from being a totally sensible and effective way of developing code, it's also a good idea to get familiar with the concept now, before Visual Studio.net 2005 appears; it has unit testing abilities built right into the IDE.

If you've been following all the unit testing press, you could be forgiven for thinking that unit testing is a practice confined to C# programmers. Thankfully that's not the case. You can build nUnit tests in Visual Basic.NET quite easily, and setting up the IDE to help you out is a snap too. Let's take a look at that first.

When you create or load a new project, a drop down just underneath the toolbar allows you to choose the solution configuration settings. In a nutshell you can configure compiler and debugger settings across the entire solution and then save them into a named configuration.

By default you have just two; Debug and Release. The Debug configuration settings turn off some optimizations of the compiler and lace your compiled binaries with symbols to make the IDE debugger's job easier and its output more intuitive. If you're working on a rich Windows forms app though, both these settings present you with a problem; hitting F5 runs the application, and not any unit tests that you may have built. That's where the configuration manager comes in. We can use it to add a third configuration to the project, perhaps called Unit Tests, that runs the nUnit test case browser instead of starting the project itself. Selecting Configuration Manager from the drop down pops up the standard configuration manager dialog box. Again it has a combo box allowing you to select the configuration that you want to edit.

That list also has an option labelled <New...>. Selecting it pops up yet another dialog asking you what you want to call the new configuration and whether you want to base it on an existing one.

In the example here I've named my new configuration UnitTests and I've indicated that I want to create that config by copying the Debug configuration. So far so good. Clicking OK returns you to the Configuration manager but this time around shows the UnitTests configuration. We're still not quite there yet though. The new UnitTests configuration is just a copy of the Debug one. If we were to select this configuration either using the configuration manager or the combo box at the top of the IDE all that we'll get is another Debug build, but just called something different. What we want is for the UnitTests config to actually run up the nUnit test browser.

This is easy to change. First we need to close down the configuration manager and choose UnitTests instead of Debug from the combo box within the IDE. This activates the new configuration, but more importantly means that any changes we make now to the solution or project properties affect this specific configuration. Right clicking on the project in the solution explorer, then choosing Properties from the drop down menu takes us into the Project Properties dialog. You'll see two sets of configuration properties listed; Common Properties, and Configuration Properties. Obviously we want to change the Configuration Properties, and more specifically the Debugging settings.

As the screenshot shows, by default Visual Studio expects to launch the project when you hit F5. However, to work with nUnit just select Start External Program and then click on the button to the right of the option to find the Nunit-gui.exe program that Nunit installs when you first set it up.  With that selected, Hit the OK button.

If you now press F5 to run your project you'll find the nUnit test browser runs instead of the application.

So now you have a project with 3 working configurations. Hitting F5 with the release configuration selected compiles and builds the project then runs it without any debugging information. Selecting the Debug configuration results in a build with debugging information included that you can step through in the IDE. Selecting UnitTests on the other does exactly the same as the Debug configuration, but when you Hit F5 the nUnit test browser runs, and you can see the output of your tests. You can even single step through the code just like in a normal debug build if you feel the need to (although the hardcore eXtreme Programming advocates out there will tell me off for mentioning that - never use the debugger, blah blah).

In the next post, I'll show you how to actually write up nUnit tests in Visual Basic and all the various options available to you for running and controlling the tests within nUnit.

 

 


11:34:51 AM    comment []


Click here to visit the Radio UserLand website. © Copyright 2004 Pete Wright.
Last update: 01/07/2004; 16:45:15.
This theme is based on the SoundWaves (blue) Manila theme.
June 2004
Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      
May   Jul