在本示例中,将创建一个简单的测试条件,该条件验证“结果集”中所返回的列数是否符合预期。您可以使用该条件来确保存储过程的协定正确无误。
//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 方法执行测试条件的主要任务。您可以重写此方法以验证是否符合预期的条件。此方法提供两个参数:
您必须设置一个类库,而且该类库中必须包含要签名的测试条件。签名操作可在“签名”选项卡上的项目属性中执行。