Nirmal
Nirmal

Reputation:

How can I write .NET unit tests?

How do I write unit tests in .NET?

Specifically Visual Studio 2005?

Upvotes: 2

Views: 447

Answers (8)

mmacaulay
mmacaulay

Reputation: 3049

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.

Upvotes: 0

Chris Burgess
Chris Burgess

Reputation: 5835

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.

Upvotes: 2

roundcrisis
roundcrisis

Reputation: 17806

+1 on MbUnit and efinetly use testdriven.net

i found it much faster particularly for datamodel tests

Upvotes: 0

David Pokluda
David Pokluda

Reputation: 10971

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.

Upvotes: 3

Sklivvz
Sklivvz

Reputation: 31163

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.

Upvotes: 0

Brendan Enrick
Brendan Enrick

Reputation: 4307

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.

Upvotes: 1

jonsequitur
jonsequitur

Reputation: 546

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.

Upvotes: 3

Bob King
Bob King

Reputation: 25866

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

Upvotes: 2

Related Questions