Unit tests for generic methods

06/28/2012

5 minutes to read

In this article

You can generate unit tests for generic methods exactly as you do for other methods, as described in How to: Create and Run a Unit Test. The following sections provide information about and examples of creating unit tests for generic methods.

Type Arguments and Type Constraints

When Visual Studio generates a unit test for a generic class, such as MyList<T>, it generates two methods: a generic helper and a test method. If MyList<T> has one or more type constraints, the type argument must satisfy all the type constraints. To make sure that the generic code under test works as expected for all permissible inputs, the test method calls the generic helper method with all the constraints that you want to test.

Examples

The following examples illustrate unit tests for generics:

Editing Generated Test Code. This example has two sections, Generated Test Code and Edited Test Code. It shows how to edit the raw test code that is generated from a generic method into a useful test method.

Using a Type Constraint. This example shows a unit test for a generic method that uses a type constraint. In this example, the type constraint is not satisfied.

Example 1: Editing Generated Test Code

The test code in this section tests a code-under-test method named SizeOfLinkedList(). This method returns an integer that specifies the number of nodes in the linked list.

The first code sample, in the section Generated Test Code, shows the unedited test code as it was generated by Visual Studio Premium or Visual Studio Ultimate. The second sample, in the section Edited Test Code, shows how you could make it test the functioning of the SizeOfLinkedList method for two different data types, int and char.

This code illustrates two methods:

a test helper method, SizeOfLinkedListTestHelper<T>(). By default, a test helper method has "TestHelper" in its name.

a test method, SizeOfLinkedListTest(). Every test method is marked with the TestMethod attribute.

Generated Test Code

The following test code was generated from the SizeOfLinkedList() method. Because this is the unedited generated test, it must be modified to correctly test the SizeOfLinkedList method.

In the preceding code, the generic type parameter is GenericParameterHelper. Whereas you can edit it to supply specific data types, as shown in the following example, you could run the test without editing this statement.

Edited Test Code

In the following code, the test method and the test helper method have been edited to make them successfully test the code-under-test method SizeOfLinkedList().

Test Helper Method

The test helper method performs the following steps, which correspond to lines in the code labeled step 1 through step 5.

Create a generic linked list.

Append four nodes to the linked list. The data type of the contents of these nodes is unknown.

Assign the expected size of the linked list to the variable expected.

Compute the actual size of the linked list and assign it to the variable actual.

Compare actual with expected in an Assert statement. If the actual is not equal to the expected, the test fails.

Test Method

The test method is compiled into the code that is called when you run the test named SizeOfLinkedListTest. It performs the following steps, which correspond to lines in the code labeled step 6 and step 7.

Specify <int> when you call the test helper method, to verify that the test works for integer variables.

Specify <char> when you call the test helper method, to verify that the test works for char variables.

Each time the SizeOfLinkedListTest test runs, its TestHelper method is called two times. The assert statement must evaluate to true every time for the test to pass. If the test fails, it might not be clear whether the call that specified <int> or the call that specified <char> caused it to fail. To find the answer, you could examine the call stack, or you could set breakpoints in your test method and then debug while running the test. For more information, see How to: Debug while Running a Test in an ASP.NET Solution.

Example 2: Using a Type Constraint

This example shows a unit test for a generic method that uses a type constraint that is not satisfied. The first section shows code from the code-under-test project. The type constraint is highlighted.

Test Project

As with all newly generated unit tests, you must add non-inconclusive Assert statements to this unit test to make it return useful results. You do not add them to the method marked with the TestMethod attribute but to the "TestHelper" method, which for this test is named DataTestHelper<T>().

In this example, the generic type parameter T has the constraint where T : Employee. This constraint is not satisfied in the test method. Therefore, the DataTest() method contains an Assert statement that alerts you to the requirement to supply the type constraint that has been placed on T. The message of this Assert statement reads as follows: ("No appropriate type parameter is found to satisfies the type constraint(s) of T. " + "Please call DataTestHelper<T>() with appropriate type parameters.");

In other words, when you call the DataTestHelper<T>() method from the test method, DataTest(), you must pass a parameter of type Employee or a class derived from Employee.