Interested in a
Personal Demo ?


Name* :
Please Enter your Name
Company E–Mail* :
Please Enter a Valid Email

Testimonials


"Typemock has helped us get some difficult to test code under test, as well as speed up the writing of some business rules/classes. The new version is quite impressive with the suggested tests. Any tool that helps you write more (and better!) tests has got to help make better quality code." 
Stephen Price Lead Developer at Unlisted Marketplace
Success stories

PurchaseOptions


US Toll Free
866–895–4680
Outside US
+44–203–150–0293
Get your printable quote
Buy online

 

How to Start Unit Testing .NET Projects

 

A Unit Testing Guide

By Gil Zilberfeld, former Product Manager, Typemock (This article first appeared in Sys-Con on June 3, 2011)

 

Testing Framework for .NET:

 

The first unit testing 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 the Gallio project) or xUnit. There are minor differences between the different frameworks, but the main features are the same. Typemock Isolator also includes the SmartRunner, which will run tests for you inside Visual Studio. 

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 for .NET:


The next thing you need is a .Net mocking framework, like Typemock 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()

AAA Methodology:

 

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

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 about the Isolator's AAA API

 

How to Start Unit Testing .NET Projects  Download Free Trial    Basic unit testing: Mocking and
Faking
     

Free Webinar: Introduction to Unit Testing
Writing Your First Unit Test in C# Difference Between Integration Tests & Unit Tests