Interested in a
Personal Demo ?


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

Testimonials


We need to maintain legacy applications that were not built to facilitate unit testing. Without Typemock we would be completely unable to effectively test these applications. Typemock boosts productivity because you do not have to explicitly code from the ground up with testing in mind, which can be difficult to handle. Typemock has been able to cover up the coupling and dependency sins that we all have leaked into our applications from time to time.  
Edmund D. Gorski, President, CodePoets, Inc.
Success stories

PurchaseOptions


US Toll Free
877–634–0165
Outside US
+44–2035146725
Get your printable quote
Buy online starting from $249

 

New Isolator is out! Give it a free (unit)-test-drive now! 

How to Start Unit Testing .NET Projects

A Unit Testing Guide

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

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 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:
The next thing you need is a
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()

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.

 

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