Export (0) Print
Expand All

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 NotInheritable Class DataSourceAttribute _
	Inherits Attribute

The DataSourceAttribute type exposes the following members.

Public methodDataSourceAttribute(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.
Public methodDataSourceAttribute(String, String)Initializes a new instance of the DataSourceAttribute class. This instance will be initialized with a connection string and table name.
Public methodDataSourceAttribute(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.

Public propertyConnectionStringGets a value representing the connection string for the data source.
Public propertyDataAccessMethodGets the method used to access the data source.
Public propertyDataSourceSettingNameGets a value indicating the setting name used to identify data source connection information stored in a configuration file.
Public propertyProviderInvariantNameGets a value representing the data provider of the data source.
Public propertyTableNameGets a value indicating the table name providing data.
Public propertyTypeIdWhen implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.)

Public methodEqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Public methodGetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIsDefaultAttributeWhen overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Inherited from Attribute.)
Public methodMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

Public fieldStatic memberDefaultDataAccessMethodRepresents the default data access method. This field is read-only.
Public fieldStatic memberDefaultProviderNameRepresents the default data provider name. This field is read-only.

Explicit interface implemetationPrivate methodSystem#Runtime#InteropServices#_Attribute#GetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetationPrivate methodSystem#Runtime#InteropServices#_Attribute#GetTypeInfoRetrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute.)
Explicit interface implemetationPrivate methodSystem#Runtime#InteropServices#_Attribute#GetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetationPrivate methodSystem#Runtime#InteropServices#_Attribute#InvokeProvides access to properties and methods exposed by an object. (Inherited from 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 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:



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

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




Jorg Bott



Pedro Ruivo



Mandar Samant



Russell King



Jun Cao



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
                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)

            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.
© 2015 Microsoft