High on my list of stuff I need to do is NUnit. Last I tried NUnit, it was wonderful and infuriating. I could run tests or debug code, but not both. My, how times have changed. I must conclude NUnit rocks!
NUnit is for making automated tests of .NET code. Each test is a piece of C# code with a special [Test] attribute. There are 1000 quick start tutorials out there, so I won’t bore you. Do a Google search, and you’re good. And the NUnit quick start docs are also quite good.
Here’s the secret sauce though: TestDriven.NET, a plug-in for Visual Studio 2005. Sadly, it’s closed source. However, it is free, and well worth it. TestDriven.NET provides a context menu in Visual Studio. (See screen shots here.) Right-click on the NUnit project in the solution window, and choose Run Test(s). Or choose a file in the project to only run those tests. I spent quite a while trying to get it to auto-load in Visual Studio before I found this note on TestDriven’s site saying it wasn’t needed, that TestDriven would load things when they were run. (It makes it a bit more difficult to disable the plug-in without uninstalling though.)
Now here’s where it gets really good. One of the options on the menu is “Test With -> Debugger”. I absolutely despised debugging NUnit tests before. I’d launch the GUI, run the test, get a red light, then wonder why. Then I’d have to build some stupid console or windows app to run the NUnit test project’s dll, and step through it. Or I’d insert a bunch of alert(“not this”) or MessageBox.Show(“here”) code, then remember to rip it back out when I found the problem. (And put it back in when that wasn’t it.) Not fun. Alas, that is no more. I set my break points, choose “Test With” -> “Debugger”, and step into the code. Very nice…
Ok, I’m a bit annoyed that I can’t do fix-and-resume, but I understand why. If the process running it was vshost, yeah, it’d work fine. It’s probably run by nunit-console though.
TestDriven.NET installs NUnit, NCover, a code coverage tool (e.g. which portions of my code did it test, which portions are not tested), and MSBee, a tool for using MSBuild with .NET 1.1.
I found this post as I was constructing the blog post. It details how to run NUnit tests on private members using reflection. (Yeah, you could do this in regular code too, but please don’t.)
Another note: the Model-View-Presenter pattern works really nicely with this train of thought, because it separates all the “do it” logic from the “show it” logic within the GUI layer. Thus, I create an NUnit View, and I’ve got great unit testing of my interface. Very smooth.