Greg
Greg

Reputation: 34838

Which Unit Test framework should I start with for C# in Visual Studio 2008? (Windows Forms app)

QUESTION - If I'm starting a Windows Forms application in C# using Visual Studio 2008, which unit testing framework should I use?

There seems to be one build into VS2008? Or should I look into something like NUnit? Or is NUnit what is used in VS2008 under the bonnet? What's the most popular.

Would be good to have something that allows for: (a) mocking/stubs, and (b) ability to assert exceptions

thanks

Upvotes: 9

Views: 1134

Answers (6)

Dale
Dale

Reputation: 13044

If it just a simple application, the MSTest framework built into VS2008 should tick all the boxes. It is not the same as NUnit, though a lot of people (mistakenly) refer to it as if they were the same thing.

I have never used it for mocking, so maybe someone else can elaborate on that. It generates a separate project in your solution that is just for the purpose of testing. This is a good explanation of how to use it.

alt text
(source: geekzone.co.nz)

Edit: Doing a bit more digging, I came across this comparison of MSTest and NUnit.

Drawbacks of NUnit Framework:

  • Installation of NUnit comes in a separate MSI.
  • No Integration with Visual Studio.
  • Requires writing test cases manually.
  • No auto generation of code.
  • Requires opening separate window (NUnit Console or GUI) to execute test cases.
  • Ordering of test cases execution not available.
  • No inbuilt feature to debug test cases.
  • No inbuilt feature to enable/disable test cases.
  • No inbuilt feature to give additional information of test cases like stack trace, Trace Information etc.
  • No inbuilt feature to sort test cases based on Computer Name, Class Name and Host Type etc.

That's from the comparison article I linked to.

I have never used NUnit, only MSTest for C# and JUnit for Java, so I am kind of biased in that respect. MSTest has always worked really well for me for WinForms, with features like being able to run the tests individually, show really detailed reports (with individual trace logs) and autogenerate all the boilerplate test code and do all those kind of things that make VS2008 such a brilliant IDE. By the sounds of it, NUnit has worked well for others and they have their reasons why they like it.

Unless you have a particular reason to take the NUnit/Testdriven.NET approach, like you need a certain feature, or you just prefer that way of setting up tests and trying to integrate it back into VS, then I don't see any reason not to just use MSTest which works right out of the box.

Upvotes: 5

Gishu
Gishu

Reputation: 136683

You didn't mention if you were going to be test-driving code..

  • Go with NUnit if you're not familiar with any of the xUnit frameworks. Smallest learning curve. It has been around for longer. Charlie Poole is active on NUnit (open source) and has a good history of good updates.
  • VS2008 has MSTest integrated inside it. It's not the same as NUnit. I've never had reason to switch from NUnit. I'd argue that NUnit definitely has the biggest user-base.
  • NUnit has something bundled for Mocks but I'd definitely point you to Moq. Rhino Mocks has been the top dog for a while now but Moq has an easier learning curve (from online docs) although it may not always handle edge scenarios... yet IMHO
  • Asserting exceptions is supported in both.

I'd recommend getting a thin book 'Pragmatic Unit Testing in C# with NUnit' and going through it atleast once.

On the NUnit v MSTest line, I'm not fit to comment. 0 flying-time with MSTest.

  • Although I hear that VS2010 will offer trinkets like automated code-coverage reports.. only if you've MSTest tests. But that's the kind of argument I keep hearing in MSTests favor.. more on the lines of IDE Support and integration. There are great add-ins like Resharper and TestDriven that can patch that in but at a price.
  • Also IMHO, Microsoft's unit testing tooling has a subtle way of leading you down the dark path. so be careful. See my comment to Dale on this thread.

Upvotes: 1

Phillip Ngan
Phillip Ngan

Reputation: 16126

Use MSTest. It's built into VS2008 and has support in the IDE for creating test functions. If and when you "outgrow" MSTest, then you should look at NUnit or the more modern xUnit. You can expect exceptions in MSTest by decorating with the ExpectedException attribute.

[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void Should_throw_ArgumentNullException_when_the_Order_is_null()
{
    OrderProcessor processor = new OrderProcessor();
    processor.ProcessOrder(null);
}

Mocking frameworks are independent of your choice of testing framework. Popular choices for mocking framework are: Moq and Rhino Mocks.

Upvotes: 3

Steve
Steve

Reputation: 12054

We've started with MSTest. It has an advantage that is (mildly) difficult to replicate in NUnit. It allows you to access private members of the class. This is invaluable when you are checking state. For instance, say that I have a function that puts entries into a dictionary that is not exposed. My understanding, is that with NUnit you have to use reflection to get to the dictionary, or add a getter for the sole purpose of testing. Here, you can just check the dictionary. Very easy and very clean. It also allows you to test private functions, which I love (I know some people don't believe in this). While I don't love MSTest, that feature makes testing much easier.

VS integration is also nice.

Upvotes: 5

Chris Missal
Chris Missal

Reputation: 6153

I would recommend using NUnit for your testing framework. It's very lightweight and easy to ship around if you need to get it set up on a build server. This isn't the case using MSTest. As for mocking/isolation frameworks, Rhino Mocks has the largest user base and you're likely to find answers to your questions quickest with Rhino Mocks.

Upvotes: 8

Andrew Keith
Andrew Keith

Reputation: 7563

i would say, for simplicity, start with the one built into visual studio 2008. NUnit is fantastic and i use it alot, so you can probably move on to NUnit once your comfortable with writing unit tests.

Upvotes: 2

Related Questions