Saturday 11 May 2013

Data-Driven Test with visual studio 2012 - how to feed test method with sql server datas.

Data-driven test is one way to test our system with predefined values. 

In this post, i will test a method named "Divide" and feed it with values defined in a SQL server database table. The test engine will loop as many as row exist in this table. 

The method "Divide" looks like 

public class Helper
{
        public int Divide(int a, int b)
        {
            return a / b;
        }



After creating a Unit test projet in visual studio 2012, add a dll referencing the class to be tested.
By default, a class "UnitTest1" has been created and contains a test method TestMethod1.

 [TestClass]
public class UnitTest1
{
        [TestMethod]
        public void TestMethod1()
        {

        }


1- The first step to do is to add a TestContext property in this class. TestContext is an object in which general/global properties are accessible and available for the current run.
Our class becomes :

 [TestClass]
public class UnitTest1
{
         private TestContext _testContext;
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return _testContext;
            }
            set
            {
                _testContext = value;
            }
        }

        [TestMethod]
        public void TestMethod1()
        {

        }


Sql script used in this example :
create table DivideBoundaryValues(num int,den int)
insert into DivideBoundaryValues values(0,1),(1,2),(4,2)


Then decorate the TestMethod1 with DataSource Attribute

  [DataSource("System.Data.SqlClient", "server=WIN-0LQSDB1L1T3\\SQL2008D;Database=DataDrivenStore;User Id=SSS;Password=SSS", "DivideBoundaryValues", DataAccessMethod.Sequential)]
     
        [TestMethod]
        public void TestMethod1()
        {

         }

The first parameter in the constructor is the provider. System.Data.SqlClient is a good candidate in our case.
The second parameter is a standard connection string like the one in the app.config or web.config.
The third one is the name of the table in which values will be retrieved.
The last one tell that data order will be used  as they are ordered in the table.

Add the System.Data dll in the UnitTest1 project. It will be usefull in the next step ( when using DataRow).

Finally , the TestMethod1() looks like :
  [DataSource("System.Data.SqlClient", "server=WIN-0LQSDB1L1T3\\SQL2008D;Database=DataDrivenStore;User Id=Administrator;Password=jY4i42u123456;integrated security=true", "DivideBoundaryValues", DataAccessMethod.Sequential)]
     
        [TestMethod]
        public void TestMethod1()
        {
            Helper helper = new Helper();
            var a = (int)TestContext.DataRow[0];
            var b = (int)TestContext.DataRow[1];
            Assert.AreEqual(2, helper.Divide(a,b));
        } 


where DataRow[0] is the first col in the table, DataRow[1] the second, and so on...

As we can see in this figure, VS executes 3 times the TestMethod1, one row one call.

Data Source can be also retrieved from CSV, XML File or a custom provider .

For the CSV case , just change the datasource attribute to :
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV","|DataDirectory|\\data.csv","data.csv",DataAccessMethod.Sequential), DeploymentItem("C:\\Sources\\data.csv")] 
In this example : datas are stored in a file named  data.csv
The only constraint when using CSV is that lines must be comma-separated.

1 comment:

  1. Hi there to every one, the contents present at this web page are actually amazing for people knowledge, well, keep up the good work fellows.
    Historical Option Prices

    ReplyDelete