views:

1530

answers:

6

My platform: Visual C# 2008 Express Edition with NUnit 2.2.7

I have a solution with my code in one project and my NUnit unit tests in a different project in the same solution.

I have been struggling hard to debug and single-step through the NUnit tests. I found some references online that suggested calling the following:

NUnit.ConsoleRunner.Runner.Main(args);

But this doesn't even compile - it has the compiler error:

Error 1 The type or namespace name 'Runner' does not exist in the namespace 'NUnit.ConsoleRunner' (are you missing an assembly reference?)

I've added every assembly reference I could find, to no effect.

Finally, this is what I have hacked together and it works, but perhaps you good readers could suggest a better solution:

1) In my test project, the class name of a test case that I want to debug is MyTestClass. It has a [TestFixtureSetUp] method named Init() and the actual test case is in [Test] function MyTest()

2) In my code project, I have a console program TestProgram.cs which compiles to an EXE.

In TestProgram.cs, I call the test cases in the following way

// First instantiate the test class
MyTestClass tc = new MyTestClass();

// Call the TestFixtureSetup method
tc.Init();

// Now call the actual test
tc.MyTest();

This works and I can debug and single step through the test cases.

If anyone has any better suggestions using Visual Studio 2008 Express without paying for additional plugins, I appreciate your advice.

A: 

At my shop, we're doing it exactly as you do. On the downside, this means that setup might not be exactly as NUnit does it. On the upside, it is quite simple to do and allows one to create a minimal environment to reproduce the bug.

David Schmitt
+2  A: 

Since you're using an Express version of Visual Studio, you cannot use the free TestDriven.NET add-in. That's unfortunate, because that really is the best tool I've used to debug unit tests.

The Runner class can be found in the nunit-console-runner.dll assembly, so be sure to add a reference to that. I'm not sure there's anything it does that your simple entry point does not, but it is probably best to use it to be safe and for future compatibility.

One other (dirty) option worth mentioning is for your unit test to invoke Debugger.Attach(). I haven't tried this with an express install, but you should be prompted to attach a debugger when your unit test runs. You can then select your existing VS instance and debug with that.

HTH, Kent

Kent Boogaart
Have you used ReSharper's test runner? I find that much nicer than TestDriven.NET - although of course it's not free.
Jon Skeet
Hi Jon, Yes I have and I agree it's a nice runner. However, I'm not a fan of ReSharper due to its resource requirements. I tried it for a significant period of time (> 3 months) and found it caused me a lot of pain.
Kent Boogaart
A: 

I found some references online that suggested calling the following: NUnit.ConsoleRunner.Runner.Main(args); But this doesn't even compile - it has the compiler error:

JFYI You need to add a .NET DLL reference to nunit-console-runner assembly in the NUnit DLL bundle ( I have 2.2.4.0 ) Seems like the class has also been renamed but its there if you need it.

NUnit.ConsoleRunner.ConsoleUi.Main(args);
Gishu
+3  A: 

Jon as a good way but not the more automatic one ;)

Here is what I already say on SO:

You can create a blank project (choose console application for example) and in the property of the project you can select DEBUG tag and select Start External Program. Put the path of Nunit. Than, in the start option, the command line arguments select the DLL that contain all your test (mine is always in the nunit\bin...). Than select Enable unmanaged code debugging and you will be able to start the project inside VS and even use the debugger step-by-step.

This way, you do not need macro or anything to be done every time, it's just a normal F5 :)

Daok
Do you even need a separate project? Can't you just set these options on your unit test project?
Kent Boogaart
Kent is correct. (Unless of course the Express Edition doesn't allow starting an external program when pressing F5)
Pedro
In Visual C# 2010 Express, you cannot use "Start External Program". From http://msdn.microsoft.com/en-us/library/68c8335t.aspx "The Express editions, such as Microsoft Visual C# Express, do not support changing the StartAction property; this is only supported in the full Visual Studio product."
Burly
+3  A: 

Have a look at http://nunit.com/blogs/?p=28

This works a treat.

BlackWasp
+1, good find, thanks
jcollum
Thanks BlackWasp. This link addresses the issue of the paid editions having "Start External Program" as an option while the Express editions do not.
Burly
A: 

If you have ReSharper installed it should be able to detect your [Test] fixtures and place an icon to the left of each method to run, and to the left of each test fixture to run all. I prefer this way.

Most of the people at work also have TestDriven.NET, which for some reason isn't on my machine, so they wonder what's going on. It's kinda funny.

tsilb