다음을 통해 공유


방법: 데이터베이스 단위 테스트 디자이너에 테스트 조건 추가

업데이트: 2007년 11월

테스트 조건을 만드는 데 사용된 TestCondition 클래스는 완전히 확장할 수 있습니다. 다음 절차에서는 데이터베이스 단위 테스트 디자이너에 나타나는 테스트 조건을 만드는 방법을 설명합니다.

테스트 조건을 만들려면

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

  2. 다음 어셈블리에 대한 참조를 추가합니다.

    • Microsoft.VisualStudio.TeamSystem.Data.UnitTesting.dll

    이 파일을 추가하려면 [Program Files]\Microsoft Visual Studio 9.0\DBPro로 이동해야 합니다. 여기서 [ProgramFiles]는 프로그램 파일 폴더를 나타냅니다.

  3. 다음 코드 예제에 표시된 대로 TestCondition 클래스에서 클래스를 파생합니다.

    using Microsoft.VisualStudio.TeamSystem.Data.UnitTesting.Conditions;
    using System.ComponentModel;
    
    [DisplayName("NewTestCondition")]
    public class NewTestCondition:TestCondition
    {
       // Additional implementation to be added here
    }
    
  4. 강력한 이름으로 어셈블리에 서명합니다. 자세한 내용은 방법: 강력한 이름으로 어셈블리 서명을 참조하십시오.

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

  6. gacutil /i를 사용하여 어셈블리를 전역 어셈블리 캐시에 추가합니다. 자세한 내용은 전역 어셈블리 캐시 도구(Gacutil.exe)를 참조하십시오.

    참고:

    gacutil 명령을 실행하려면 Visual Studio 2005의 명령 프롬프트 창을 통해 실행해야 합니다. 이 창을 열려면 시작을 클릭하고 모든 프로그램과 Microsoft Visual Studio 2005를 차례로 가리킨 다음 Visual Studio Tools를 클릭합니다. 표준 명령 프롬프트 창을 사용하는 경우 gacutil.exe 위치를 가리키도록 PATH 환경 변수를 편집해야 합니다. 일반적으로 이 위치는 [Program Files]\Microsoft Visual Studio 9.0\SDK\v2.0\Bin입니다.

  7. 새 테스트 조건을 등록합니다. 자세한 내용은 방법: 새 테스트 조건 등록을 참조하십시오.

예제

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

//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 속성을 사용하여 예상 열 수를 지정할 수 있습니다. 각 속성에 대해서는 다음과 같은 세 가지 특성이 추가됩니다.

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

  • 속성의 표시 이름

  • 속성 설명

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

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

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

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

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

참고 항목

작업

방법: 새 테스트 조건 등록