About the author

My name is Donovan Brown and I am a Principal DevOps Manager with Microsoft with a background in application development. I also run one of the Nation’s fastest growing online registration sites for motorsports events DLBRacing.com. When I am not writing software I race cars for fun. DLBRacing.com has given me the opportunity to combine my two passions writing software and racing cars.

Explanation:

Creating a custom validation rule for web test is extremely simple. Simply create a new public class that derives from Microsoft.VisualStudio.TestTools.WebTesting.ValidationRule and override the Validate method. If you create the class in your test project it will become immediately available the next time you try to add a validation rule to a web request. If you created the class in a separate class library simply add a reference to that class library in your test project.

This particular validation rule has the five following properties:

Query – This is the query to be executed on the database connection. Only the first column of the first record is used in the comparison of this validation rule. The query can have a single where condition in the where clause that uses a context parameter value. For example “Select Name from Table1 where ID={0}”. At runtime the validation rule will look up the value of the provided context parameter and replace {0} with the value stored in the context parameter. Using a context parameter is completely optional.

Connection String – The connection string to a SQL Server database. This can also be a context parameter entered in {{ContextParameterName}} format. Otherwise you may simple enter a literal string.

Where Clause Context Parameter Name – The context parameter to be used to replace the {0} if any of the query.

Ignore Case – Identifies if case should be ignored or not during the string comparison.

Expected Value – The value to compare the first column of the first row too. This can also be a context parameter entered in {{ContextParameterName}} format. Otherwise you may simple enter a literal string.