Can you bind Validation Rules to datasources?

And the answer is yes. I came accross this question many time so thought of putting a blog on it. Below are the steps to follow on how to bind datasource to validation rule. Its not different than any other databinding in test.

1. You should have a webtest. To know how to create a webtest follow below link msdn.microsoft.com/en-us/library/ms182538.aspx

2. Add a datasource to the webtest

  • In the Web Test Editor, right-click the top node of the Web test and then click Add Data Source.
  • The New Test Data Source Wizard appears.
  • In the Data source name box type a name for your data source.
  • In the Data source type list click Database.
  • Click Next.
  • Select an existing connection, or follow these steps:
  • Click New Connection.
  • The Choose Data Source dialog box appears.
  • In the Data source list, click the appropriate source for your database.
  • (Optional) In the Data provider list, click the appropriate provider for your database.
  • (Optional) Clear the Always use this selection check box.
  • Click Continue.
  • The Connection Properties dialog box appears.
  • Enter the information to connect to your database.
  • (Optional) Click the Test Connection button to verify your connection to the database.
  • Click OK to close the Connection Properties dialog box.
  • The connection string to access the database is entered in the wizard.
  • Click Next.
  • Under Choose from the available tables for this data source, select the check box next to each table that you want to include in the data source.
  • Click Finish.

3. Add validation rule

  •  In the webtest editor, select the web request
  • right click and select "Add validation rule". It will display a dialog box
  • In the Add Validation Rule dialog box, select a rule to configure, for eg. Find text and instead of typing string use databinding by adding {{datasourcename.fieldname}} 
  • Format should be exactly that I used. {{datasourcename.fieldname}} 
  • select ok to close the dialog and you have binded your validation rule to a datasource. Your test is ready to run