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.
Prerequisites
To complete this walkthrough, you will need:
Microsoft Visual Studio 2005 Team Edition for Software Testers
Either Microsoft Access or Microsoft Excel to provide data for at least one of the test methods.
A Visual Studio 2005 solution that contains a test project.
Create the App.config File
To add an app.config file to the project
If your test project already has an app.config file, go to Define a Custom Configuration Section.
Right-click your test project in the Solution Explorer, point to Add, and then click New Item.
The Add New Item window opens.
Select the Application Configuration File template and click Add.
Define a Custom Configuration Section
Examine the app.config file. It contains at least the XML declaration and a root element.
To add the custom configuration section to the app.config file
The root element of app.config should be the
configuration
element. Create aconfigSections
element within theconfiguration
element. TheconfigSections
should be the first element in the app.config file.Within the
configSections
element, create asection
element.In the
section
element, add an attribute calledname
and assign it a value equalmicrosoft.visualstudio.testtools
. Add another attribute calledtype
and assign it a value equalMicrosoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
The section
element should look similar to this:
<section name="microsoft.visualstudio.testtools" type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
Define Connection Strings
The connection strings define provider specific information for accessing data sources. Connection strings defined in configuration files provide reusable data provider information across an application. In this section, you create two connection strings that will be used by data sources that are defined in the Custom Configuration Section.
To define connection strings
After the
configSections
element, create aconnectionStrings
element.Within the
connectionStrings
element, create twoadd
elements.In the first
add
element, create the following attributes and values for a connection to a Microsoft Access database:
Attribute | Values |
---|---|
|
|
|
|
|
|
In the second add
element, create the following attributes and values for a connection to a Microsoft Excel spreadsheet:
|
|
|
|
|
|
The connectionStrings
element should look similar to this:
<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>
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
After the
connectionStrings
element, create amicrosoft.visualstudio.testtools
element. This section was created in Define a Custom Configuration Section.Within the
microsoft.visualstudio.testtools
element, create adataSources
element.Within the
dataSources
element, create twoadd
elements.In the first
add
element, create the following attributes and values for a Microsoft Access data source:
Attribute | Values |
---|---|
|
|
|
|
|
|
|
|
In the second add
element, create the following attributes and values for a Microsoft Excel data source:
|
|
|
|
|
|
|
|
The microsoft.visualstudio.testtools
element should look similar to this:
<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>
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>
Create a Unit Test Using Data Sources Defined in app.config
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
Create a Microsoft Access database named
testdatasource.mdb
.Create a table and name it
MyDataTable
intestdatasource.mdb
.Create two fields in
MyDataTable
namedArg1
andArg2
using the Number data type.Add five entities to
MyDataTable
with the following values forArg1
andArg2
, respectively: (10,50), (3,2), (6,0), (0,8) and (12312,1000).Save and close the database.
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
Create a Microsoft Excel spreadsheet named
data.xls
.Create a sheet named
Sheet1
if it does not already exist indata.xls
.Create two column headers and name them
Val1
andVal2
inSheet1
.Add five entities to
Sheet1
with the following values forVal1
andVal2
, respectively: (1,1), (2,2), (3,3), (4,4) and (5,0).Save and close the spreadsheet.
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
Add a unit test to the test project.
For more information, see How to: Author a Unit Test.
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"]); } } }
Examine the DataSource attributes. Notice the setting names from the app.config file.
Build your solution and run MyTestMethod and MyTestMethod2 tests.
Important
Deploy items like data sources so that they are accessible to the test in the deployment directory. For more information, see Test Deployment.
See Also
Tasks
How to: Create a Test Project
How to: Generate a Unit Test
How to: Author a Unit Test
How to: Configure a Data-Driven Unit Test
Concepts
Coding a Data-Driven Unit Test