Running the Tests Using the GUI
For now, simply click the Run button. Your one test should fail, leaving your screen looking like Figure 3
. Notice that both the status bar on the right side, as well as the circle corresponding to the individual test method on the left, turn red. This indicates that the test failed, which is not surprising because nothing has been implemented yet. Let's implement the method, as well as some more tests to further exercise the functionality.
Rounding Out the Class
Add the code in Listing 2
(C#) or Listing 3
(VB) to the ArrayListUtilityTests class, below the original test method, RemoveDuplicatesNoElementsInInput
Now, recompile the project. The NUnit GUI should automatically refresh and load the assembly that contains the new tests. Verify that you can see five tests in the tree view on the left. Click Run and verify that all five tests fail.
Now let's implement the function that will make the tests pass. The implementation is fairly simple and straightforward.
public static ArrayList RemoveDuplicateTokens(
ArrayList inputArrayList )
ArrayList purgedArrayList = new ArrayList();
foreach ( object item in inputArrayList )
if ( ! purgedArrayList.Contains( item ) )
purgedArrayList.Add( item );
In VB .NET:
Public Shared Function RemoveDuplicateTokens( _
ByVal InputArrayList As ArrayList) As ArrayList
Dim purgedArrayList As ArrayList
Dim item As Object
purgedArrayList = New ArrayList
For Each item In inputArrayList
If Not purgedArrayList.Contains(item) Then
If you run the NUnit GUI, all the tests should now pass and the status bar should be green. Congratulations, you have created your first unit tests and corresponding class.
The power of having an automated suite of tests to run against code at any time should now be evident. Although this example is fairly simple, you can now apply these simple concepts to start utilizing unit testing on your own code.