简体   繁体   中英

How can I create NUnit tests with ReSharper?

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM