更新:2007 年 11 月
用于创建测试条件的 TestCondition 类是完全可扩展的。以下过程说明如何创建将显示在“数据库单元测试设计器”中的测试条件。
创建测试条件
在 Visual Studio 中创建一个类库项目。
添加对以下程序集的引用:
- Microsoft.VisualStudio.TeamSystem.Data.UnitTesting.dll。
若要添加此文件,必须浏览至 [Program Files]\Microsoft Visual Studio 9.0\DBPro,其中 [Program Files] 表示您的“Program Files”文件夹。
从 TestCondition 类派生您的类,如下面的代码示例所示:
using Microsoft.VisualStudio.TeamSystem.Data.UnitTesting.Conditions; using System.ComponentModel; [DisplayName("NewTestCondition")] public class NewTestCondition:TestCondition { // Additional implementation to be added here }
用强名称对程序集进行签名。有关更多信息,请参见如何:使用强名称为程序集签名。
生成类库。
通过使用 gacutil /i 将该程序集添加到全局程序集缓存中。有关更多信息,请参见全局程序集缓存工具 (Gacutil.exe)。
说明:
在运行 gacutil 命令之前,请在 Visual Studio 2005 中使用命令提示符窗口运行该命令。若要打开此窗口,请单击“开始”,指向“所有程序”,指向“Microsoft Visual Studio 2005”,再单击“Visual Studio Tools”。如果使用标准的命令提示符窗口,则必须编辑 PATH 环境变量,使其指向 gacutil.exe 的位置。此位置通常为 [Program Files]\Microsoft Visual Studio 9.0\SDK\v2.0\Bin。
注册新的测试条件。有关更多信息,请参见如何:注册新的测试条件。
示例
在本示例中,将创建一个简单的测试条件,该条件验证“结果集”中所返回的列数是否符合预期。您可以使用该条件来确保存储过程的协定正确无误。
//ResultSetColumnCountCondition
//Sample custom test condition
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.ComponentModel;
using System.ComponentModel.Design;
using TestTools = Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TeamSystem.Data.UnitTesting;
using Microsoft.VisualStudio.TeamSystem.Data.UnitTesting.Conditions;
namespace MyTestConditions
{
[DisplayName("ResultSet Column Count")]
public class ResultSetColumnCountCondition : TestCondition
{
private int _resultSet;
private int _count;
private int _batch;
public ResultSetColumnCountCondition()
{
_resultSet = 1;
_count = 0;
_batch = 1;
}
//method you need to override
//to perform the condition verification
public override void Assert(DbConnection validationConnection, ExecutionResult[] results)
{
//call base for parameter validation
base.Assert(validationConnection, results);
//verify batch exists
if (results.Length < _batch)
throw new TestTools.AssertFailedException(String.Format("Batch {0} does not exist", _batch));
ExecutionResult result = results[_batch - 1];
//verify resultset exists
if (result.DataSet.Tables.Count < ResultSet)
throw new TestTools.AssertFailedException(String.Format("ResultSet {0} does not exist", ResultSet));
DataTable table = result.DataSet.Tables[ResultSet - 1];
//actual condition verification
//verify resultset column count matches expected
if (table.Columns.Count != Count)
throw new TestTools.AssertFailedException(String.Format(
"ResultSet {0}: {1} columns did not match the {2} columns expected",
ResultSet, table.Columns.Count, Count));
}
//this method is called to provide the string shown in the
//test conditions panel grid describing what the condition tests
public override string ToString()
{
return String.Format(
"Condition fails if ResultSet {0} does not contain {1} columns",
ResultSet, Count);
}
//below are the test condition properties
//that are exposed to the user in the property browser
#region Properties
//property specifying the resultset for which
//you want to check the column count
[Category("Test Condition")]
[DisplayName("ResultSet")]
[Description("ResultSet Number")]
public int ResultSet
{
get { return _resultSet; }
set
{
//basic validation
if (value < 1)
throw new ArgumentException("ResultSet cannot be less than 1");
_resultSet = value;
}
}
//property specifying
//expected column count
[Category("Test Condition")]
[DisplayName("Count")]
[Description("Column Count")]
public int Count
{
get { return _count; }
set
{
//basic validation
if (value < 0)
throw new ArgumentException("Count cannot be less than 0");
_count = value;
}
}
#endregion
}
}
自定义测试条件的类是从 TestCondition 基类继承的。由于自定义测试条件具有其他属性,因此用户可以在注册自定义测试条件之后从“属性”窗口配置它。在本示例中,将添加两个属性。自定义测试条件的用户可以使用“ResultSet”属性来指定应当检验哪个结果集内的列数。自定义测试条件的用户可以使用“Count”属性来指定预期的列计数。针对每个属性 (Property) 添加了以下三个属性 (Attribute):
类别名称(有助于对属性进行组织)。
属性的显示名称。
属性的说明。
针对这些属性执行了一些基本的验证,以确认“ResultSet”属性的值不小于一,而且“Count”属性的值大于零。
Assert 方法执行测试条件的主要任务。您可以重写此方法以验证是否符合预期的条件。此方法提供两个参数:
第一个参数是用来检验测试条件的数据库连接。
第二个也是最重要的参数是结果数组,它针对所执行的每个批处理返回单个数组元素。在本版本中,每个测试脚本仅支持一个批处理操作。因此,测试条件将总是检查第一个数组元素。数组元素中包含数据集,数据集内又包含测试脚本的返回结果集。本示例中的代码检查数据集内的数据表是否包含适当的列数。有关更多信息,请参见 DataSet。
您必须设置一个类库,而且该类库中必须包含要签名的测试条件。签名操作可在“签名”选项卡上的项目属性中执行。