BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Testing
.NET 2.0+

Debugging NUnit Tests in C# Express

Microsoft Visual C# Express is a free integrated development environment for developing software using the C# programming language. A limitation of the Express edition is that it does not support add-ins, making debugging tests more difficult.

Visual C# Express

Microsoft's Visual Studio Express Editions are cut-down versions of the full Microsoft Visual Studio integrated development environment (IDE). Although restricted, the Express editions allow you to develop fully-functioning Windows-based and web-based software that can be deployed commercially. Unfortunately, the free versions do not include support for automated unit testing, which means that a third-party testing framework, such as NUnit, must be used.

In most situations, using a test runner, such as the graphical user interface provided by NUnit, alongside your IDE is sufficient. However, sometimes it is necessary to run your tests within the scope of a debugger, so that you can monitor variable values and understand why a failure is occurring. As there is no test runner within C# Express, and as you cannot install add-ins that may support one, debugging tests within the IDE can be tricky.

In this article I will explain how you can create a simple project within your solution that permits debugging of NUnit tests. The technique should also work with other testing frameworks, though I have not tested this.

Creating the Test Runner Project

This section describes how to create the test runner project within your solution. If you do not have a test project with which to practise, download the sample code using the link at the top of the article. The sample includes a basic test project and the test runner project.

To create the test runner project within your solution, add a new console application, naming the new project, "TestRunner". The project will use NUnit's console-base test runner software to perform the testing to add a reference to "nunit-console-runner.dll" and add the following using statement to the Program class in the TestRunner project:

using NUnit.ConsoleRunner;

We can now add some code to the Main method in our Program class. All that we wish to do is call the Main method of the NUnit test runner, passing in any arguments that we received. This allows us to use the NUnit functionality from our test runner whilst running under the debugger. Add the following to the Main method:

Runner.Main(args);

Finally, we need to tell our TestRunner project where our tests are. We can do this by modifying the command line arguments that are passed to the console application when it is started in debug mode. To add the filename of the compiled DLL that contains the tests, right-click on the name of the TestRunner project in the Solution Explorer and choose "Properties" from the context-sensitive menu. In the project's property pages, choose the Debug tab and type the path to the test DLL in the Command line arguments box.

Test project location

Running Tests Within C# Express

We can now run our tests within Visual C# Express. First ensure that the Output Window is visible. Next, set "TestRunner" as the startup project for the solution and press F5. The results of the test run will be displayed in the Output Window. If you are using the downloaded sample code, the results will be similar to those shown below. This shows a failure in the test named, "FailingTest" within the "SampleTests.cs" file at line 21.

##############                F A I L U R E S              #################
1) Tests.SampleTests.FailingTest :

at
D:\NUnitCSharpExpress\Tests\SampleTests.cs(21)
############################################################################
Executed tests       : 1
Ignored tests        : 0
Failed tests         : 1
Unhandled exceptions : 0
Total time           : 0.400576 seconds
############################################################################

Debugging Tests Within C# Express

Once you have successfully run your tests using the TestRunner project, you can try debugging tests. To do so, add a breakpoint within the test code that you wish to debug. Run the tests by pressing F5. When the breakpoint is encountered you can step through the test code and the code under test, using the normal debugging tools provided by the IDE.

27 March 2011