How to: Add a Test Condition to Database Unit Test Designer

The TestCondition class used to create test conditions is completely extensible. The following procedure explains how to create a test condition to appear in Database Unit Test Designer.

To create a test condition

  1. In Visual Studio, create a Class Library project.

  2. Add a reference to the following assembly:

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

    To add this file, you must browse to [Program Files]\Microsoft Visual Studio 8\DBPro, where [ProgramFiles] represents your Program Files folder.

  3. Derive your class from the TestCondition class, as shown in the following code example:

    using Microsoft.VisualStudio.TeamSystem.Data.UnitTesting.Conditions;
    using System.ComponentModel;
    public class NewTestCondition:TestCondition
       // Additional implementation to be added here
  4. Sign the assembly with a strong name. For more information, see How to: Sign an Assembly with a Strong Name.

  5. Build the class library.

  6. Add the assembly to the global assembly cache by using gacutil /i. For more information, see Global Assembly Cache Tool (Gacutil.exe).


    Before you run the gacutil command, run it using the command-prompt window in Visual Studio 2005. To open this window, click Start, point to All Programs, point to Microsoft Visual Studio 2005, and click Visual Studio Tools. If you use the standard Command Prompt window, you must edit the PATH environment variable to point to the gacutil.exe location. Typically, this location is [Program Files]\Microsoft Visual Studio 8\SDK\v2.0\Bin.

  7. Register the new test condition. For more information, see How to: Register a New Test Condition.


In this example, you create a simple test condition that verifies that the number of columns returned in the ResultSet is what you expect. You can use this condition to make sure that the contract for a stored procedure is correct.

//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")]
        [Description("ResultSet Number")]
        public int ResultSet
            get { return _resultSet; }

                //basic validation
                if (value < 1)
                    throw new ArgumentException("ResultSet cannot be less than 1");

                _resultSet = value;

        //property specifying
        //expected column count
        [Category("Test Condition")]
        [Description("Column Count")]
        public int Count
            get { return _count; }

                //basic validation
                if (value < 0)
                    throw new ArgumentException("Count cannot be less than 0");

                _count = value;


The class for the custom test condition inherits from the base TestCondition class. Because of the additional properties on the custom test condition users can configure the condition from the Properties window after they have registered the condition. In this example, you add two properties. Users of the custom test condition can use the ResultSet property to specify for which result set the column count should be verified. They can use the Count property to specify the expected column count. Three attributes are added for each property:

  • The category name, which helps organize the properties.

  • The display name of the property.

  • A description of the property.

Some basic validation is performed on the properties to verify that the value of the ResultSet property is not less than one and that the value of the Count property is greater than zero.

The Assert method performs the primary task of the test condition. You override this method to validate that the expected condition is met. This method provides two parameters:

  • The first parameter is the database connection that is used to validate the test condition.

  • The second and most important parameter is the results array, which returns a single array element for each batch that was executed. In this release, only a single batch is supported for each test script. Therefore, the test condition will always examine the first array element. The array element contains a DataSet that, in turn, contains the returned result sets for the test script. In this example, the code verifies that the data table in the DataSet contains the appropriate number of columns. For more information, see DataSet.

You must set the class library that contains your test condition to be signed, which you can do in the project's properties on the Signing tab.

See Also

Other Resources

How to: Register a New Test Condition