Thursday, 28 April 2016

Data Driven Testing in .Net Based Application

Overview of Data-Driven Unit Tests

A data-driven unit test is a unit test that is run repeatedly for each row in a data source.
A common scenario for using data-driven unit tests is the use of multiple input values to test an API. Instead of writing multiple unit tests that call the API, each with a new set of inputs, or creating an array in a unit test and using looping code, you can instead write a single unit test method that exercises the API. You can then retrieve data from the rows of a database table, for example, to pass to successive calls of that test method.
You could use this technique to test an application that is used by different users, each of whom has a different role. For each user, one row in the data source would indicate the expected response, based on role. The test would then test the application by running functionality for each user and verify that the response produced matches the expected response.
Creating Data-Driven Unit Tests
You can create a data-driven unit test in either of two ways:

How to: Configure a Data-Driven Unit Test

You can configure data-driven unit tests in the following ways:
  • Set properties on the test using the Properties window. For more information, see the section "Setting Properties for Data-Driven Unit Tests."
  • In the source code for the unit test, provide a [DataSource] attribute, in which you specify the data that is required to access the data table. For more information, see Coding a Data-Driven Unit Test.
  • In the source code for the unit test, provide a [DataSource] attribute. But instead of using this attribute to supply the data-table access information, specify an entry from the microsoft.visualstudio.TestTools/dataSources section in the application-configuration file for the test assembly. This method gives you the flexibility to change the data-table access information without recompiling the unit test. For more information, see Walkthrough: Using a Configuration File to Define a Data Source.

Setting Properties for Data-Driven Unit Tests

You can configure a data-driven unit test by starting with any existing unit test—either generated or hand-authored—and setting specific properties on the test.

To configure a data-driven unit test

  1. Select the test in Test Manager window or Test View window.
  2. Press F4 to open the Properties window.
    The properties for the unit test are displayed in the Properties window.
  3. For the unit test, set the following properties.
    Data Connection String
To edit the Data Connection String property, click the property in the Properties window and then click the ellipsis (…). This opens the Choose Data Source dialog box, which lists several possible data sources, including ODBC, Microsoft SQL Server, and Microsoft Access. Choosing a data source opens a dialog box specific to the type of data source; you use this dialog box to configure the connection properties for the data source. When you have finished configuring the data connection, the connection string appears as the value for Data Connection String. This string is also stored as an attribute of the unit test method.

Data Provider Name
This value is set as a result of your setting the Data Connection String. Therefore, the Data Provider Name property is read-only in the Properties window. However, you can change the Data Provider Name value in an attribute of the unit test method.
Data Table Name
You can select a data table after you have established a connection to a data source. When you click the drop-down list in the values column of the Properties window, the tables in the connected database are listed. The table you select from this list is the table whose rows will be retrieved when the unit test is run. As with other properties such as Data Connection StringData Table Name is stored as an attribute of the unit test method.
Data Access Method
For the Data Access Method, select either Sequential or Random; the default value is Sequential. This setting represents the order in which records are retrieved from the table in the data source and is used when the unit test is run repeatedly.

Coding a Data-Driven Unit Test

A unit test functions as data-driven test if it has the attributes that a data-driven unit test requires. You can assign these attributes and their values either by using the Properties window or by adding the attributes directly to the test's code.
For more information on configuring a unit test as data-driven by editing its properties, see How to: Configure a Data-Driven Unit Test.
This topic describes how to code a unit test as a data-driven unit test, using the DataSource attribute and the TestContext class.

Using Data from a Data Source

When a data-driven unit test is running, data is retrieved from the rows of a data source. The data then is available to the running unit test through the DataRow and DataConnection properties of the TestContext class.
In the following example, DataRow is of the type DataRow, and LastName is the name of a valid column in the row associated with the current iteration of the data-driven test. 
TestContext.DataRow["LastName"]
While LastName refers to a column by name, you can also refer to columns by column number.
For each row in the table, any number of columns can be accessed. You can, for example, retrieve several columns of data at once, use them in a calculation, and then compare the result with a final column that contains an expected return value.

Coding a Data-Driven Unit Test

To create a data-driven unit test, you can start with either a unit test that you have created by hand or a generated unit test. For more information, see How to: Author a Unit Test and How to: Generate a Unit Test.
To configure your existing unit test, add attributes that define the data source you want it to use, the way you want that data to be accessed, and the table whose rows you want your test to use as input. For more information on configuring these attributes, see How to: Configure a Data-Driven Unit Test.
For example, the following code is from a data-driven unit test that uses data from the Northwind database.
[TestClass]
    public class TestClass
    {
        private TestContext m_testContext;
        public TestContext TestContext
        {
            get { return m_testContext; }
            set { m_testContext = value; }
        }
        [TestMethod]
        [DeploymentItem("FPNWIND.MDB")]
        [DataSource("System.Data.OleDb", "Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=\"FPNWIND.MDB\"", "Employees", DataAccessMethod.Sequential)]
        public void TestMethod()
        {
            Console.WriteLine( "EmployeeID: {0}, 
LastName: {1}", TestContext.DataRow["EmployeeID"],  TestContext.DataRow["LastName"] );
        }
    }

The code within the test method in this example uses values from the LastName and EmployeeID columns in the "Employees" table of the data source. The test method accesses these values through a TestContext property, which is defined in the test class that contains the method.

Walkthrough: Using a Configuration File to Define a Data Source

This walkthrough illustrates how to use a data source defined in an app.config file for unit testing. You will learn how to create an app.config file that defines a data source that can be used by the DataSourceAttribute class. Tasks presented in this walkthrough include the following:
  • Creating an app.config file.
  • Defining a custom configuration section.
  • Defining connection strings.
  • Defining the data sources.
  • Accessing the data sources using the DataSourceAttribute class.

To add an app.config file to the project

  1. If your test project already has an app.config file, go to Define a Custom Configuration Section.
  2. Right-click your test project in the Solution Explorer, point to Add, and then click New Item.
    The Add New Item window opens.
  3. Select the Application Configuration File template and click Add.

Define Data Sources

The data sources section contains four attributes that are used by the test engine to retrieve data from a data source.
  • name defines the identity used by the DataSourceAttribute to specify which data source to use.
  • connectionString identifies the connection string created in the previous Define Connection Strings section.
  • dataTableName defines the table or sheet that holds the data to use in the test.
  • dataAccessMethod defines the technique for accessing data values in the data source.
In this section, you will define two data sources to use in a unit test.

To define data sources

  1. After the connectionStrings element, create a microsoft.visualstudio.testtools element. This section was created in Define a Custom Configuration Section.
  2. Within the microsoft.visualstudio.testtools element, create a dataSources element.
  3. Within the dataSources element, create two add elements.
  4. In the first add element, create the following attributes and values for a Microsoft Access data source:
The final app.config file should look similar to this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="microsoft.visualstudio.testtools" type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> 
    </configSections>
    <connectionStrings>
        <add name="MyJetConn" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\testdatasource.mdb; Persist Security Info=False;" providerName="System.Data.OleDb" />
        <add name="MyExcelConn" connectionString="Dsn=Excel Files;dbq=data.xls;defaultdir=.; driverid=790;maxbuffersize=2048;pagetimeout=5" providerName="System.Data.Odbc" />
    </connectionStrings>
    <microsoft.visualstudio.testtools>
        <dataSources>
            <add name="MyJetDataSource" connectionString="MyJetConn" dataTableName="MyDataTable" dataAccessMethod="Sequential"/>
            <add name="MyExcelDataSource" connectionString="MyExcelConn" dataTableName="Sheet1$" dataAccessMethod="Sequential"/>
        </dataSources>
    </microsoft.visualstudio.testtools>
</configuration>
Now that an app.config file has been defined, you will create a unit test that uses data located in the data sources that are defined in the app.config file. In this section, we will:
  • Create the data sources found in the app.config file.
  • Use the data sources in two test methods that compare the values in each data source.

To create a Microsoft Access data source

  1. Create a Microsoft Access database named testdatasource.mdb.
  2. Create a table and name it MyDataTable in testdatasource.mdb.
  3. Create two fields in MyDataTable named Arg1 and Arg2 using the Number data type.
  4. Add five entities to MyDataTable with the following values for Arg1 and Arg2, respectively: (10,50), (3,2), (6,0), (0,8) and (12312,1000).
  5. Save and close the database.
  6. Change the connection string to point to the location of the database. Change the value of Data Source to reflect the location of the database.

To create a Microsoft Excel data source

  1. Create a Microsoft Excel spreadsheet named data.xls.
  2. Create a sheet named Sheet1 if it does not already exist in data.xls.
  3. Create two column headers and name them Val1 and Val2 in Sheet1.
  4. Add five entities to Sheet1 with the following values for Val1 and Val2, respectively: (1,1), (2,2), (3,3), (4,4) and (5,0).
  5. Save and close the spreadsheet.
  6. Change the connection string to point to the location of the spreadsheet. Change the value of dbq to reflect the location of the spreadsheet.

To create a unit test using the app.config data sources

  1. Add a unit test to the test project.
    For more information, see How to: Author a Unit Test.
  2. Replace the auto-generated contents of the unit test with the following code:
    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    
    namespace TestProject1
    {
         [TestClass]
        public class UnitTest1
        {
            private TestContext context;
    
            public TestContext TestContext
            {
                get { return context; }
                set { context = value; }
            }
    
            [TestMethod()]
            [DeploymentItem("MyTestProject\\testdatasource.mdb")]
            [DataSource("MyJetDataSource")]
            public void MyTestMethod()
            {
                int a = Int32.Parse(context.DataRow["Arg1"].ToString());
                int b = Int32.Parse(context.DataRow["Arg2"].ToString());
                Assert.AreNotEqual(a, b, "A value was equal.");
            }
    
            [TestMethod()]
            [DeploymentItem("MyTestProject\\data.xls")]
            [DataSource("MyExcelDataSource")]
            public void MyTestMethod2()
            {
                Assert.AreEqual(context.DataRow["Val1"], context.DataRow["Val2"]);
            }
        }
    }
    
  3. Examine the DataSource attributes. Notice the setting names from the app.config file.
  4. Build your solution and run MyTestMethod and MyTestMethod2 tests.
Deploy items like data sources so that they are accessible to the test in the deployment directory. For more information, see Test Deployment.

No comments:

Post a Comment