Browse DevX
Sign up for e-mail newsletters from DevX


Getting Started with Unit Testing : Page 3

Learn unit testing now and find the problems in your code before things get out of control.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Creating Your First Test Class
Now that you've correctly installed and referenced NUnit within the project, you can create the sample class and its unit tests.

Delete Class1, the default class that Visual Studio inserted into the project. Add a new class called ArrayListUtility. The ArrayListUtility class is where the method to test will live.

Inside the new class, import the System.Collections namespace.

In C#:

using System; using System.Collections;


Imports System.Collections

Next, create a new static method that will take in an ArrayList, and return a new ArrayList that is purged of duplicates.

In C#:

public static ArrayList RemoveDuplicateTokens( ArrayList inputArrayList ) { return null; }


Public Shared Function RemoveDuplicateTokens( _ ByVal inputArrayList As ArrayList) As ArrayList Return Nothing End Function

Notice that the methods currently do not do anything except return null. Why? The unit tests will build up the functionality within this class.

Build your project to make sure that it compiles.

Next, add a new class named ArrayListUtilityTests. This class will holds the unit tests for the ArrayListUtility class. Import the NUnit framework and System.Collections namespace into ArrayListUtilityTests.

In C#:

using System; using System.Collections; using NUnit.Framework;


Imports System.Collections Imports NUnit.Framework

Now it's time to add the first test. Open ArrayListUtilityTests and add the following code snippet:

In C#:

[Test] public void RemoveDuplicatesNoElementsInInput() { ArrayList outputArrayList = ArrayListUtility.RemoveDuplicateTokens( new ArrayList() ); Assert.AreEqual( 0, outputArrayList.Count, "ArrayList Count" ); }


<Test()> Public Sub _ RemoveDuplicatesNoElementsInInput() Dim outputArrayList As ArrayList Dim inputArrayList As ArrayList inputArrayList = New ArrayList outputArrayList = ArrayListUtility.RemoveDuplicateTokens _ (inputArrayList) Assert.AreEqual(0, outputArrayList.Count, _ "ArrayList count") End Sub

Take a moment to notice a few things about this test method. First, it's decorated with the Test attribute. Using attributes is the means by which you inform NUnit that the method is a test. Second, it is a regular method that returns nothing, as indicated by the void keyword in the method signature. Within the NUnit framework, test methods are regular methods with void return types. The only action performed by this test is to verify that if passed an empty ArrayList, an empty ArrayList comes out. It is a simple test, but it does just enough to get started.

Finally we'll mark the test class as an actual NUnit test class. In the ArrayListUtilityTests class, apply the TestFixture attribute as shown below.

In C#:

[TestFixture] public class ArrayListUtilityTests


<TestFixture()> Public Class ArrayListUtilityTests

Now the test class is ready for its first run. Compile the project and make sure that there are no errors or warnings. At this time, you need to load NUnit in order to run the test class.

Thanks for your registration, follow us on our social networks to keep up-to-date