Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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)

System.Object
  System.Attribute
    Microsoft.VisualStudio.TestTools.UnitTesting.DataSourceAttribute

<AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple := False)>
Public NotInheritable Class DataSourceAttribute
	Inherits Attribute

NameDescription
System_CAPS_pubmethodDataSourceAttribute(String)

Initializes a new instance of the DataSourceAttribute class. This instance will be initialized with a data provider and connection string associated with the setting name.

System_CAPS_pubmethodDataSourceAttribute(String, String)

Initializes a new instance of the DataSourceAttribute class. This instance will be initialized with a connection string and table name.

System_CAPS_pubmethodDataSourceAttribute(String, String, String, DataAccessMethod)

Initializes a new instance of the DataSourceAttribute class. This instance will be initialized with a data provider, connection string, data table and data access method to access the data source.

NameDescription
System_CAPS_pubpropertyConnectionString

Gets a value representing the connection string for the data source.

System_CAPS_pubpropertyDataAccessMethod

Gets the method used to access the data source.

System_CAPS_pubpropertyDataSourceSettingName

Gets a value indicating the setting name used to identify data source connection information stored in a configuration file.

System_CAPS_pubpropertyProviderInvariantName

Gets a value representing the data provider of the data source.

System_CAPS_pubpropertyTableName

Gets a value indicating the table name providing data.

System_CAPS_pubpropertyTypeId

(Inherited from Attribute.)

NameDescription
System_CAPS_pubmethodEquals(Object)

(Inherited from Attribute.)

System_CAPS_pubmethodGetHashCode()

(Inherited from Attribute.)

System_CAPS_pubmethodGetType()

(Inherited from Object.)

System_CAPS_pubmethodIsDefaultAttribute()

(Inherited from Attribute.)

System_CAPS_pubmethodMatch(Object)

(Inherited from Attribute.)

System_CAPS_pubmethodToString()

(Inherited from Object.)

NameDescription
System_CAPS_pubfieldSystem_CAPS_staticDefaultDataAccessMethod

Represents the default data access method. This field is read-only.

System_CAPS_pubfieldSystem_CAPS_staticDefaultProviderName

Represents the default data provider name. This field is read-only.

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 DataSourceAttribute.

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:

[DataSource("dataSourceNameFromConfigFile")]

System_CAPS_noteNote

Different data providers use different connection strings. The name of the provider 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 (NIB).

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

Legacy Code Example

The following code contains the class and method to test.

Imports System

Namespace BankAccountNS
    Public Class BankAccount
        Private customerName As String
        Private bal As Double

        Public Sub New(ByVal customerName2 As String, ByVal balance As Double)
            customerName = customerName2
            bal = balance
        End Sub

        Public ReadOnly Property Balance() As Double
            Get
                Return bal
            End Get
        End Property

        Public Sub Debit(ByVal amount As Double)
            If amount < 0 Then
                Throw New ArgumentOutOfRangeException("amount")
            End If
            bal -= amount
        End Sub
    End Class
End Namespace

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

Name

Balance

Amount

Jorg Bott

100

25

Pedro Ruivo

70

60

Mandar Samant

75

71.25

Russell King

159

158

Jun Cao

11.99

11.22

Note that the DataAccessMethod is sequential.

Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports BankAccountNS
Imports System

Namespace TestProject1
    <TestClass()> _
    Public Class BankAccountTest

        Private testContextInstance As TestContext
        Public Property TestContext() As TestContext
            Get
                Return testContextInstance
            End Get
            Set(ByVal Value As TestContext)
                testContextInstance = Value
            End Set
        End Property

        <TestMethod()> _
        <DataSource("System.Data.OleDb", _        "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""D:\sample.mdb""", _        "Table1", DataAccessMethod.Sequential)> _
        Public Sub DebitTest()
            Dim customerName As String = testContextInstance.DataRow("Name").ToString()

            Dim balance As Double = Convert.ToDouble(testContextInstance.DataRow("Balance"))
            Dim amount As Double = Convert.ToDouble(testContextInstance.DataRow("Amount"))

            Dim NewBalance As Double = balance - amount

            Dim target As BankAccount = New BankAccount(customerName, balance)
            target.Debit(amount)

            Assert.AreEqual(NewBalance, target.Balance, 0.0)
        End Sub
    End Class
End Namespace

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

Return to top
Show: