Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

DataSourceAttribute Class

Provides data source-specific information for data-driven testing. This class cannot be inherited.

Namespace: Microsoft.VisualStudio.TestTools.UnitTesting
Assembly: Microsoft.VisualStudio.QualityTools.UnitTestFramework (in microsoft.visualstudio.qualitytools.unittestframework.dll)

[AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple=false)] 
public sealed class DataSourceAttribute : Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple=false) */ 
public final class DataSourceAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple=false) 
public final class DataSourceAttribute extends Attribute

The DataSourceAttribute class provides two ways to specify data source information for data-driven tests. The first way specifies information through a connection string, provider information, and source table name passed to the DataSource attribute.

Connection String Example:

[DataSource("Provider=SQLOLEDB.1;Data Source=MySource;Integrated] Security=SSPI;Initial Catalog=MyCatalog;Persist Security Info=False", "MyTable")]

The second way passes a single argument to the attribute that specifies the configuration setting located in the app.config file.

Configuration Setting Example:



Different providers use different connection strings. The provider itself is a part of connection string.

For more information about using the app.config file for specifying a data source, see Walkthrough: Using a Configuration File to Define a Data Source.

For more information about data-driven tests, see Overview of Data-Driven Unit Tests.

For more information about using attributes, see Extending Metadata Using Attributes.

The following code contains the class and method to test.

using System;

namespace BankAccountNS
    public class BankAccount
        private string custName;
        private double bal;

        public BankAccount(string customerName, double balance)
            custName = customerName;
            bal = balance;

        public double Balance
        { get { return bal; } }

        public void Debit(double amount)
            if (amount < 0)
                throw new ArgumentOutOfRangeException("amount");
            bal -= amount;

The following test will pass. It uses the sample.mdb access database that contains the following data in Table1.




Jorg Bott



Pedro Ruivo



Mandar Samant



Russell King



Jun Cao



Note that the DataAccessMethod is sequential.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using BankAccountNS;
using System;

namespace MyCSTestProject
    public class BankAccountTest
        private TestContext testContextInstance;
        public TestContext TestContext
            get { return testContextInstance; }
            set { testContextInstance = value; }

           "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"C:\\sample.mdb\"",
        public void DebitTest()
            string customerName = testContextInstance.DataRow["Name"].ToString();

            double bal = Convert.ToDouble(testContextInstance.DataRow["Balance"]);
            double amt = Convert.ToDouble(testContextInstance.DataRow["Amount"]);

            double newBalance = bal - amt;

            BankAccount target = new BankAccount(customerName, bal);

            Assert.AreEqual(newBalance, target.Balance, .00);


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

© 2015 Microsoft