views:

867

answers:

2

Hi,

I'm trying to get into unit testing with C#. Various people told me to go with NUnit since it's better than MSTest (apparently, I have no idea) and it also has very good support in ReSharper which I'm using.

Now I've never written a unit test before in my life (bear with me, I'm a university student). ReSharper has this nice CreateUnitTests context menu option that I've seen others (casually looking over the shoulder) use to great success. You right-click in a method, select CreateUnitTests and there you go, a test skeleton is created. You just fill in the important bits.

Now when I try the same, ReSharper wants me to create a new Test Project... and when I let it, it creates (what I'm assuming) a MSTest project with obviously a MSTest test template. But I already have a class libarary project which references "nunit.framework" and has a few NUnit tests that ReSharper is more than willing to run. Still, it only ever creates MSTest test templates, and only ever in special "Test Project" projects.

What am I doing wrong? Am I doing something wrong at all, or is creating NUnit test templates not possible with ReSharper? I've searched the net and read the documentation of ReSharper and NUnit and I still can't figure out is this even possible or what.

I'd be grateful if anyone could provide me with a sort of guide to using ReSharper + NUnit.

EDIT: I'm using ReSharper 4.5 and NUnit 2.5.3

EDIT2: Apparently I'm an idiot. CreateUnitTests is not part of ReSharper, but part of Visual Studio and thus only ever works with MSTest.

+4  A: 

Hi there.

In your test project which Resharper created, remove the reference to the Microsoft unit testing DLL (I don't recall the name off hand, but it's quite a long name).

Then add a new reference - nunit.framework.dll, you should find it on the first tab of the Add Reference dialog.

Add using NUnit.Framework to to the unit test class file.

You then need to change attributes:

[TestClass] to [TestFixture]
[TestMethod] to [Test]

So if you end up with a MSTest project, use the above steps to get NUnit instead.

NOTE: Resharper 4.5 onwards does have native support for running MSTest as well as NUNit tests. So you could try that instead.

Cheers. Jas.

Jason Evans
But adding a new test with CreateUnitTests again adds the reference to the Microsoft testing DLL and again uses TestClass instead of TestFixture etc... would I have to remove these very time? Does this mean that ReSharper can only ever create MSTest test templates?
Lucas
I'm not that familiar with Resharper's options - are you sure it's Resharper giving you the option to create unit tests? I use Visual Studio 2008 Team System and when I right-click on a method I get the option 'Create Unit Tests'. Since this is part of VS it will always use MSTest. I'm not sure if it is Resharper doing this. One to find out is disable Resharper (Tools->Addins) and create a unit test from scratch, if it's using MSTest then it's not Resharper.
Jason Evans
Ahhh, I see. Apparently I'm an idiot.
Lucas
Nah, you're not an idiot!
Jason Evans
A: 

You don't need to run any wizards to use NUnit. You can just create a class library, add a reference to NUnit and mark your tests with the corresponding attribute. The Wizards are only for MSTest and even then, not required.

Once you have the unit tests, the ReSharper test runner will detect them and on the left-hand margin you'll get some icons that will allow you to run/debug tests. See the first image here for an example:

NUnit and ReSharper

Hadi Hariri