tags:

views:

348

answers:

8

How do I write unit tests in .NET?

Specifically Visual Studio 2005?

+2  A: 

You can either use one of the Team Suite products, or use a 3rd party tool like NUnit.

Bob King
If you use NUnit, be sure to also get TestDriven.NET
plinth
+3  A: 

If you're using one of the Visual Studio Team Suite products, you reference Microsoft.VisualStudio.TestTools.UnitTesting, decorate your test classes with the TestClass attribute, and decorate your test methods with the TestMethod attribute.

The approach for NUnit is similar, except that you reference NUnit.Framework, the class will be decorated with TestFixture, and the test methods will be decorated with Test.

jonsequitur
+1  A: 

There are plenty of ways you can write units tests. Not sure if you're asking about tools to perform the unit testing or if you're wondering about the process of unit testing.

NUnit is a popular testing tool. Microsoft's MSTest which you can use along with Visual Studio Team Suite products is also very nice.

I recommend reading about continuous integration if you plan on doing a lot of testing.

As far as actually writing tests is concerned that is a very complicated topic for StackOverflow. I recommend reading articles about testing. There are plenty out there that cover this topic.

Brendan Enrick
A: 

In my opinion, MBUnit is the way to go. It's vastly superior to NUnit (very similar, but with Row Tests and Transaction support) and other MS tools (Team Suite was not really successful, I would discourage its use).

MbUnit provides advanced unit testing support with advanced fixtures to enable developers and testers to test all aspects of their software.

Sklivvz
Are you an MBUnit developer?
Greg D
No, I am a developer who likes MbUnit :)
Sklivvz
+3  A: 

If you decide for NUnit then it might be a good idea to take a look at testdriven.net since it offers a really nice integration with Visual Studio. There are multiple other unit test frameworks currently available. I would recommend you to consider xUnit. I know what you're thinking: "Why another unit test framework?". It is true that it is nothing that exceptionally better but still. Take a look at this blog post and if you decide you can use ReSharper plug-in for Visual Studio integration.

David Pokluda
A: 

+1 on MbUnit and efinetly use testdriven.net

i found it much faster particularly for datamodel tests

Miau
+2  A: 

Here's a basic idea using NUnit

Create a test project.

Reference the main project for your application in the test project.

Create a test class in the test project.

Add Imports nunit.Framework

Decorate the class with <TestFixture()>

E.g. <TestFixture()> Public Class MyTestClass ...

Create test methods, each decorated with <Test()> and add your tests:

<Test()> _
Public Sub harness()
    Assert.IsTrue(False)
End Sub

Build your test project. Open NUnit and open your test project there. Click 'run' Green is pass, Red is Fail.

The 'harness' test above is merely to prove that you've hooked everything up before you start testing your own stuff. Once it's hooked up properly (and you get a fail on the test) switch to 'true' and NUnit should show green.

From here, you start to add tests for your own project.

Gern Blandston
A: 

SpecUnit makes a good addition to NUnit for BDD style unit tests. I really like the idea of having tests in a class called something like "When_x_happens", and the methods in that class describe the expected results for when x happens - "foo_should_do_bar()". SpecUnit also has a tool for generating reports from your tests, which is a great source of documentation.

mmacaulay