Share via


How to: Create Test Conditions for the Database Unit Test Designer

This topic applies to:

Visual Studio Ultimate

Visual Studio Premium

Visual Studio 2010 Professional 

Visual Studio Express

Topic applies Topic applies Topic does not apply Topic does not apply

You can use the extensible TestCondition class to create new test conditions. For example, you might create a new test condition that verifies the number of columns or the values in a result set.

The following procedure explains how to create a test condition to appear in the Database Unit Test Designer.

To create a test condition

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

  2. On the Project menu, click Add Reference.

  3. Click the .NET tab.

  4. In the Component Name list, select Microsoft.Data.Schema.UnitTesting and Microsoft.Data.Schema, and then click OK.

  5. Derive your class from the TestCondition class.

  6. Sign the assembly with a strong name. For more information, see How to: Sign an Assembly with a Strong Name.

  7. Build the class library.

  8. Before you can use the new test condition, you must copy your signed assembly to the %Program Files%\Microsoft Visual Studio 10.0\VSTSDB\Extensions\CustomExtensions folder, where CustomExtensions is the name of the folder that you or your computer administrator have created to contain your feature extension XML files.

  9. Register the test condition. For more information, see How to: Register and Manage Feature Extensions.

Example

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

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
    }
}

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. Then, users 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 the Assert 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 more important parameter is the results array, which returns a single array element for each batch that was executed.

Only a single batch is supported for each test script. Therefore, test conditions 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

Tasks

How to: Register and Manage Feature Extensions

Walkthrough: Using a Custom Test Condition to Verify the Results of a Stored Procedure

Concepts

Define Custom Conditions for Database Unit Tests