Welcome!

.NET Authors: Elizabeth White, Lori MacVittie, Adine Deford, Greg O'Connor, Liz McMillan

Related Topics: .NET, Silverlight

.NET: Article

How to Start Unit Testing .NET Projects

A Unit Testing Guide

This article was written by Gil Zilberfeld, Product Manager, Typemock.

The first tool you need is a testing framework. You have a couple of choices there. If you are using Microsoft Visual Studio 2008 Professional or above, you already have the Microsoft unit testing framework installed. The framework comes also with Microsoft Visual Studio 2005 Team System.

You may also decide to go with one of the open source frameworks: NUnitMbUnit (now part of theGallio project) or xUnit. There are minor differences between the different frameworks, but the main features are the same. However, the Microsoft one also comes with a built in test runner. It allows you to run tests inside Visual studio.  To achieve the same ease-of-use with the open source frameworks you will need TestDriven.Net by Jamie Cansdale.

For using the attributes and API of the different frameworks, you just need to add references to them in your test project. The Microsoft test framework has a special Test Project, the other framework uses a regular DLL container for the tests.

Mocking Framework:
The next thing you need is a mocking framework, like Isolator. Follow the steps in our quick start guide how to write the first test. You'll need to add references to Isolator DLLs.

Now for your first test: In order to make it easy, let's start by testing a component you're already working on. It's very important to integrate test writing into your regular work, and that's the first step.

In your test project, create a test class, called after your class-under-test. For example if you class is called MyClass, call the test class MyClassTests.

Writing the first unit test:
Now it's time to write a test. Decide what public method you want to test, what is the specific scenario and the expected result for that scenario. Use that information to name your test. Use the following convention:

public void METHODNAME_SCENARIO_RESULT()

Now add the content of the test. A test contains three parts:

  • Arrange - setup for the test
  • Act - the actual invocation of the method
  • Assert - verification of pass/fail condition for the test

Make sure you use Assert functions to specify pass/failure functionality. You can also use Isolate.Verify for method calls as well, otherwise, the test will appear to have passed.

Run the test. If the test fails, add additional behavior setting statements with Isolator in the Arrange part, to make sure the test follows the specific scenario you're testing.

Did the test finally pass? Congratulations! You now can move to the next one.

Remember, the more tests you have, the better coverage you have, and your code becomes more stable.

•   •   •


More Stories By NCSM Strategic Marketing

Lazer Cohen writes on a variety of subjects including cloud, security, telecom, HTML5, software & software testing, bug-prevention and start-up markets. You are invited to follow @NCSM_PR