HOW TO:建立資料庫單元測試設計工具的測試條件

這個主題適用於:

Visual Studio Ultimate

Visual Studio Premium

Visual Studio 2010 Professional 

Visual Studio Express

標題適用於 標題適用於 標題不適用於 標題不適用於

您可以使用可擴充的 TestCondition 類別來建立新的「測試條件」(Test Condition)。 例如,您可以建立新的測試條件來驗證結果集中的資料行數或值。

下列程序將說明如何建立測試條件,以出現在「資料庫單元測試設計工具」中。

若要建立測試條件

  1. 在 Visual Studio 中,建立類別庫專案。

  2. 在 [專案] 功能表上,按一下 [加入參考]。

  3. 按一下 [.NET] 索引標籤。

  4. 在 [元件名稱] 清單中,選取 [Microsoft.Data.Schema.UnitTesting] 和 [Microsoft.Data.Schema],然後按一下 [確定]。

  5. TestCondition 類別衍生您的類別。

  6. 使用強式名稱簽署組件。 如需詳細資訊,請參閱 HOW TO:使用強式名稱簽署組件

  7. 建置類別庫。

  8. 使用新的測試條件之前,您必須將已簽署的組件複製到 %Program Files%\Microsoft Visual Studio 10.0\VSTSDB\Extensions\CustomExtensions 資料夾,其中 CustomExtensions 是您或您的電腦系統管理員建立來包含「擴充功能」(Feature Extension) XML 檔的資料夾名稱。

  9. 註冊測試條件。 如需詳細資訊,請參閱 HOW TO:註冊和管理功能擴充

範例

在這個範例中,您將建立的簡單測試條件,以驗證 ResultSet 中傳回的資料行數目是否如您所預期。 您可以使用這個簡單的測試條件確認預存程序的合約是否正確。

using System;
using System.Collections.Generic;
using TestTools = Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Data.Schema.UnitTesting;
using Microsoft.Data.Schema.UnitTesting.Conditions;
using Microsoft.Data.Schema.Extensibility;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using Microsoft.Data.Schema;
 
namespace TeamSystem.Data.Samples.DBUnitTesting
{
    [DatabaseSchemaProviderCompatibility(DspCompatibilityCategory.Any)]
    [DatabaseSchemaProviderCompatibility(DspCompatibilityCategory.None)]
    [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 DataException(String.Format("Batch {0} does not exist", _batch));
 
            ExecutionResult result = results[_batch - 1];
 
            //verify resultset exists
            if (result.DataSet.Tables.Count < ResultSet)
                throw new DataException(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 DataException(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) 會加入 3 個屬性 (Attribute):

  • 分類名稱,有助於組織屬性。

  • 屬性的顯示名稱。

  • 屬性的描述。

有些基本驗證是對屬性執行的,以確認 [ResultSet] 屬性值不小於 1,且 [Count] 屬性值大於 0。

Assert 方法會執行測試條件的主要工作。 您要覆寫 Assert 方法以驗證是否有符合預期條件。 這個方法提供 2 個參數:

  • 第一個參數是資料庫連接,用於驗證測試條件。

  • 第二個也是較重要的參數是結果陣列,會針對每個執行的批次傳回單一陣列元素。

每個「測試指令碼」(Test Script) 只支援單一批次。 因此,條件永遠會檢查第一個陣列元素。 陣列元素包含一個 DataSet,而 DataSet 包含測試指令碼的傳回結果集。 在這個範例中,程式碼會驗證 DataSet 中的資料表是否包含適當的資料行數目。 如需詳細資訊,請參閱 DataSet

您必須設定包含要簽署的測試條件的類別庫,您可以在 [簽署] 索引標籤的專案屬性進行這項作業。

請參閱

工作

HOW TO:註冊和管理功能擴充

逐步解說:使用自訂測試條件驗證預存程序的結果

概念

定義資料庫單元測試的自訂條件