Step 3: Writing The First Test

Next, we'll write our first test. For that we'll add a class to the project. This class can have any name and is a TestFixture. Typically you will create one such TextFixture for every class you are writing.

In this case we will assume that we have a class named Calculator which we want to test. The class may not exist yet.

To your project please add a class named "CalculatorTests". The resulting code should look like the following:

using System;

using System.Collections.Generic;

using System.Text;

 

namespace Go48.Core {

   class CalculatorTests {

   }

}

Next I need to add a using statement, so the code will now look like this (change is in bold):


using System;

using System.Collections.Generic;

using System.Text;

 

using csUnit;

 

namespace Go48.Core {

   public class CalculatorTests {

   }

}

Make sure that you don't forget to add the access modifier "public" to the class as otherwise csUnit will not be able to find it!

Next, we need to make sure that csUnit searches our class for test cases. This is achieve by decorating the class with the TestFixtureAttribute. Again the changes are in bold:

using System;

using System.Collections.Generic;

using System.Text;

 

using csUnit;

 

namespace Go48.Core {

   [TestFixture]

   public class CalculatorTests {

   }

}

Now we are ready to write our first test. Let's try to add to simple numbers:

using System;

using System.Collections.Generic;

using System.Text;

 

using csUnit;

 

namespace Go48.Core {

   [TestFixture]

   public class CalculatorTests {

      [Test]

      public void AddTwoNumbers() {

         // Step 1: Set up some objects

         Calculator calculator = new Calculator();

        

         // Step 2: Manipulate the objects

         calculator.Enter(3);

         calculator.Enter(5);

         calculator.Add();

        

         // Step 3: Assert outcome is correct

         Assert.Equals(8, calculator.Top);

      }

   }

}

This needs a bit more of an explanation. The general approach that you should take is made explicitly in this case using the comments for step 1, step 2, and step 3. Normally, you wouldn't write these comments, but just the test. For the purpose of this tutorial I added the comments to demonstrate what the recommend structure of the test should be.

First, set up the object or the small set of objects you need for your test to run. Next, invoke as few methods you can get in order to test the functionality you want to test. In this particular case, I'm assuming that I would like to have a calculator that uses Reverse Polish Notation (RPN), made famous by HP's calculators.

RPN based calculators are stack based. Therefore the Enter() operation adds one entry to the stack, in this case first the number 3 and then the number 5. This gives you two numbers on the stack, which we tell the calculator to add by submitting, guess what, the Add() operation, which concludes the second step of a test, namely manipulating the object(s) under test.

In the third step is ensuring that the outcome is the expected one. An RPN based calculator should actually remove the two values to be added from the stack, add them, and put the result back onto the stack. We don't want to remove the result from the stack, but we want to look at it, which is what the property Top is providing.

csUnit, similar to all other unit testing frameworks, provides a rich set of assertions made available in the Assert class. The typical use is Assert.Equals(expected, actual). Supported are a lot of different types including integers. In this particular test we expect 8 to be the result of the additon (use a calculator to confirm!), and we obtain the actual value using calculator.Top.

Also make sure that your test method is decorated with the TestAttribute ("[Test]"). Also the test method must be parameter-less and it must not return a value, that is it must be of type "void".

Now we have finished our test. Let's look next on the initial implementation of our calculator.


Goto Step 4: "The Initial Calculator Implementation"

Back to Step 3: "Write The Test"

Back to Start

Home

Download

Documentation

Features

Tutorials

Support

Suggest A Feature

Report A Bug

About



Web csunit.org

Sponsors:

Extreme Simplicity Logo

Agile Utilities Logo

Blue Note Ventures Logo


Sources hosted by
Get csUnit - unit testing for .NET at SourceForge.net. Fast, secure and Free Open Source software downloads



Copyright © 2002-2008 by Agile Utilities NZ Ltd. All rights reserved. Site design by Andreas Weiss. This site is protected by bot traps.