Unit Tests and Generics

Generics in .NET Framework help us to design classes and methods without any specific parameter types, allowing us to realize type safety at compile time. It means we can continue working with the class in a type-safe way, but don't have to force it to be of any specific type. Generics help us to re-use code and increase performance. Generics are mostly used in place of collections such as ArrayList, Linked List, Stacks, Queues, and other collections. This is because the collections can hold any type of items, for example, an array list can be a list of integers or it can be a list of strings. The following is an example of a generic method, which just accepts two generic values and copies the first one into the second one:

        public static void CopyItems<T>(List<T> srcList, List<T> destList)
        {
            foreach (T itm in srcList)
            {
                destList.Add(itm);
            }
        }

Here, you can notice that the type is not specified anywhere. It is generic, which is denoted by <T>. It can be an integer or string or any type that is identified when the method is called. The following code shows an example using the CopyItems generic method. The first time CopyItems is called, the listSource collection passed as the first parameter contains String items. The second time the CopyItems method is called the listSrc collection contains items of type Employee object:

        static void Main(string[] args)
        {
            List<string> listSource = new List<string>();
            listSource.Add("String1");
            listSource.Add("string2");
            List<string> listDestination = new List<string>();
            Console.WriteLine("Items count in listDestination before copying items: {0} ", listDestination.Count);
            CopyItems(listSource, listDestination);
            Console.WriteLine("Items count in listDestination after copying items: {0} ", listDestination.Count);
            Console.WriteLine("");
            List<Employee> listSrc = new List<Employee>();
            listSrc.Add(new Employee(1001, "Employee 1001"));
            listSrc.Add(new Employee(1002, "Employee 1002"));
            listSrc.Add(new Employee(1003, "Employee 1003"));
            List<Employee> listDest = new List<Employee>();
            Console.WriteLine("Items count in listDest before copying items: {0} ",  listDest.Count);
            CopyItems(listSrc, listDest);
            Console.WriteLine("Items count in listDest after copying items: {0} ", listDest.Count);
        }

The result would be the copy of objects in the destination collection, which is the second parameter, using the generic method. The output of the method after calling the generic method will be as shown in the following screenshot:

Unit Tests and Generics

Unit testing for generic methods in Visual Studio is very simple. First create a Unit Test Project if there isn't one, then right-click on the unit Test Project and add a Generic Test to the project. You will notice that the generic test template is added to the project with the default name GenericTest1.GenericTest. In the template, under Specify an existing program (a test, test harness, or test adapter) to wrap as a generic test, indicate the path and the file name of GenericExample1.exe which is the project executable. The executable file should be in the project output directory:

Unit Tests and Generics

Now the Test for generic is ready and can be run from the command line or using the Test Explorer window.

The following screenshot shows the test output for the Generic test when run from the Test Explorer window:

Unit Tests and Generics

Arguments can be passed to the Generic test while running the test. This can be done by setting the argument value under the Command line arguments to pass to the generic test section in the GenericTest1.GenericTest template. Save the file and run the test from Test Explorer. This should return the result for the test by taking the argument values.

To deploy additional files along with the generic test, choose the files using the Add option under the Additional files to deploy with this generic test section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.30.19