为数据驱动测试提供数据源特定的信息。 此类不能被继承。
继承层次结构
System.Object
System.Attribute
Microsoft.VisualStudio.TestTools.UnitTesting.DataSourceAttribute
命名空间: Microsoft.VisualStudio.TestTools.UnitTesting
程序集: Microsoft.VisualStudio.QualityTools.UnitTestFramework(在 Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 中)
语法
声明
<AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple := False)> _
Public NotInheritable Class DataSourceAttribute _
Inherits Attribute
[AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple = false)]
public sealed class DataSourceAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Method, AllowMultiple = false)]
public ref class DataSourceAttribute sealed : public Attribute
[<Sealed>]
[<AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple = false)>]
type DataSourceAttribute =
class
inherit Attribute
end
public final class DataSourceAttribute extends Attribute
DataSourceAttribute 类型公开以下成员。
构造函数
| 名称 | 说明 | |
|---|---|---|
![]() |
DataSourceAttribute(String) | 初始化 DataSourceAttribute 类的新实例。将使用与设置名称关联的数据提供程序和连接字符串来初始化该实例。 |
![]() |
DataSourceAttribute(String, String) | 初始化 DataSourceAttribute 类的新实例。用连接字符串和表名称来初始化该实例。 |
![]() |
DataSourceAttribute(String, String, String, DataAccessMethod) | 初始化 DataSourceAttribute 类的新实例。使用用于访问数据源的数据提供程序、连接字符串、数据表和数据访问方法来初始化该实例。 |
页首
属性
| 名称 | 说明 | |
|---|---|---|
![]() |
ConnectionString | 获取一个值,该值表示数据源的连接字符串。 |
![]() |
DataAccessMethod | 获取用于访问数据源的方法。 |
![]() |
DataSourceSettingName | 获取一个值,该值表示用于标识存储在配置文件中的数据源连接信息的设置名称。 |
![]() |
ProviderInvariantName | 获取一个值,该值表示数据源的数据提供程序。 |
![]() |
TableName | 获取一个值,该值表示提供数据的表名称。 |
![]() |
TypeId | 当在派生类中实现时,获取该 Attribute 的唯一标识符。 (继承自 Attribute。) |
页首
方法
| 名称 | 说明 | |
|---|---|---|
![]() |
Equals | 基础结构。返回一个值,该值指示此实例是否与指定的对象相等。 (继承自 Attribute。) |
![]() |
Finalize | 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。) |
![]() |
GetHashCode | 返回此实例的哈希代码。 (继承自 Attribute。) |
![]() |
GetType | 获取当前实例的 Type。 (继承自 Object。) |
![]() |
IsDefaultAttribute | 当在派生类中重写时,指示此实例的值是否是派生类的默认值。 (继承自 Attribute。) |
![]() |
Match | 当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。 (继承自 Attribute。) |
![]() |
MemberwiseClone | 创建当前 Object 的浅表副本。 (继承自 Object。) |
![]() |
ToString | 返回表示当前对象的字符串。 (继承自 Object。) |
页首
字段
| 名称 | 说明 | |
|---|---|---|
![]() ![]() |
DefaultDataAccessMethod | 表示默认数据访问方法。此字段为只读。 |
![]() ![]() |
DefaultProviderName | 表示默认的数据提供程序名称。此字段为只读。 |
页首
显式接口实现
| 名称 | 说明 | |
|---|---|---|
![]() ![]() |
_Attribute.GetIDsOfNames | 将一组名称映射为对应的一组调度标识符。 (继承自 Attribute。) |
![]() ![]() |
_Attribute.GetTypeInfo | 检索对象的类型信息,然后可以使用该信息获取接口的类型信息。 (继承自 Attribute。) |
![]() ![]() |
_Attribute.GetTypeInfoCount | 检索对象提供的类型信息接口的数量(0 或 1)。 (继承自 Attribute。) |
![]() ![]() |
_Attribute.Invoke | 提供对某一对象公开的属性和方法的访问。 (继承自 Attribute。) |
页首
备注
DataSourceAttribute 类提供了两种方法,用于为数据驱动测试指定数据源信息。 第一种方式通过连接字符串、提供程序信息和传递到 DataSourceAttribute 的源表名称来指定信息。
连接字符串示例:
[DataSource("Provider=SQLOLEDB.1;Data Source=MySource;Integrated] Security=SSPI;Initial Catalog=MyCatalog;Persist Security Info=False", "MyTable")]
第二种将单一自变量传送到指定位于 app.config 文件中的配置设置的属性。
配置设置示例:
[DataSource("dataSourceNameFromConfigFile")]
提示
不同的数据提供程序使用不同的连接字符串。 提供程序的名称是连接字符串的一部分。
关于为指定数据源使用 app.config 文件的更多信息,请参见 演练:使用配置文件定义数据源。
有关数据驱动测试的更多信息,请参见 Overview of Data-Driven Unit Tests。
有关使用特性的更多信息,请参见利用特性扩展元数据。
示例
下面的代码包含要测试的类和方法。
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;
}
}
}
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
下面的测试将会通过。 该测试使用 Access 数据库 sample.mdb,该数据库在 Table1 中包含以下数据。
Name |
Balance |
金额 |
|---|---|---|
Jorg Bott |
100 |
25 |
Pedro Ruivo |
70 |
60 |
Mandar Samant |
75 |
71.25 |
Russell King |
159 |
158 |
Jun Cao |
11.99 |
11.22 |
请注意,DataAccessMethod 是顺序访问方法。
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BankAccountNS;
using System;
namespace MyCSTestProject
{
[TestClass()]
public class BankAccountTest
{
private TestContext testContextInstance;
public TestContext TestContext
{
get { return testContextInstance; }
set { testContextInstance = value; }
}
[TestMethod()]
[DataSource("System.Data.OleDb",
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"C:\\sample.mdb\"",
"Table1",
DataAccessMethod.Sequential)]
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);
target.Debit(amt);
Assert.AreEqual(newBalance, target.Balance, .00);
}
}
}
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
线程安全
此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
请参见
参考
Microsoft.VisualStudio.TestTools.UnitTesting 命名空间
.gif)
.gif)
.gif)
.gif)
.gif)
.gif)
.gif)