방법: 데이터베이스 단위 테스트 디자이너용 테스트 조건 만들기

이 항목은 다음 언어에 적용됩니다.

Visual Studio Ultimate

Visual Studio Premium

Visual Studio 2010 Professional 

Visual Studio Express

항목이 적용됨 항목이 적용됨 항목이 적용되지 않음 항목이 적용되지 않음

확장 가능한 TestCondition 클래스를 사용하여 새 테스트 조건을 만들 수 있습니다. 예를 들어 결과 집합의 값이나 열 수를 확인하는 새 테스트 조건을 만들 수 있습니다.

다음 절차에서는 데이터베이스 단위 테스트 디자이너에 나타나는 테스트 조건을 만드는 방법을 설명합니다.

테스트 조건을 만들려면

  1. Visual Studio에서 클래스 라이브러리 프로젝트를 만듭니다.

  2. 프로젝트 메뉴에서 참조 추가를 클릭합니다.

  3. .NET 탭을 클릭합니다.

  4. 구성 요소 이름 목록에서 Microsoft.Data.Schema.UnitTestingMicrosoft.Data.Schema를 선택하고 확인을 클릭합니다.

  5. TestCondition 클래스에서 클래스를 파생시킵니다.

  6. 강력한 이름으로 어셈블리에 서명합니다. 자세한 내용은 방법: 강력한 이름으로 어셈블리 서명을 참조하십시오.

  7. 클래스 라이브러리를 빌드합니다.

  8. 새 테스트 조건을 사용하려면 먼저 서명된 어셈블리를 %Program Files%\Microsoft Visual Studio 10.0\VSTSDB\Extensions\CustomExtensions 폴더에 복사해야 합니다. 여기서 CustomExtensions는 사용자나 컴퓨터 관리자가 기능 확장 XML 파일을 포함하기 위해 만든 폴더의 이름입니다.

  9. 테스트 조건을 등록합니다. 자세한 내용은 방법: 기능 확장 등록 및 관리를 참조하십시오.

예제

이 예제에서는 결과 집합에 반환된 열 수가 올바른지를 확인하는 간단한 테스트 조건을 만듭니다. 이 간단한 테스트 조건을 사용하여 저장 프로시저에 대한 계약이 올바른지를 확인할 수 있습니다.

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 속성을 사용하여 필요한 열 수를 지정할 수 있습니다.

각 속성에 대해서는 다음과 같은 세 가지 특성이 추가됩니다.

  • 속성을 구성할 수 있는 범주 이름

  • 속성의 표시 이름

  • 속성 설명

ResultSet 속성의 값이 1 이상이고 Count 속성의 값이 0보다 크다는 것을 확인하기 위해 속성에 대해 일부 기본적인 유효성 검사 작업을 수행합니다.

Assert 메서드에서는 테스트 조건의 기본 작업을 수행합니다. Assert 메서드를 재정의하여 필요한 조건이 충족되는지 확인합니다. 이 메서드는 다음과 같은 두 가지 매개 변수를 제공합니다.

  • 첫 번째 매개 변수는 테스트 조건의 유효성을 검사하는 데 사용하는 데이터베이스 연결입니다.

  • 더 중요한 두 번째 매개 변수는 실행된 각 일괄 처리에 대해 단일 배열 요소를 반환하는 결과 배열입니다.

각 테스트 스크립트에 대해 하나의 일괄 처리만 지원되므로 테스트 조건은 항상 첫 번째 배열 요소를 검사합니다. 배열 요소에는 DataSet가 포함되며 DataSet에는 테스트 스크립트에 대해 반환된 결과 집합이 포함됩니다. 이 예제에서 코드는 DataSet의 데이터 테이블에 적절한 수의 열이 포함되어 있는지를 확인합니다. 자세한 내용은 DataSet를 참조하십시오.

서명할 테스트 조건이 포함된 클래스 라이브러리를 설정해야 합니다. 이 작업은 프로젝트 속성의 서명 탭에서 수행할 수 있습니다.

참고 항목

작업

방법: 기능 확장 등록 및 관리

연습: 사용자 지정 테스트 조건을 사용하여 저장 프로시저 결과 확인

개념

데이터베이스 단위 테스트의 사용자 지정 조건 정의