Generating SQL Server Test Data with Visual Studio 2010
By: Arshad Ali | Updated: 2010-12-23 | Comments (7) | Related: More > Testing
As a database developer or tester sometimes you need to have production like data in your environment for your development or testing, but you cannot have the production data because of security and privacy issues. So how you can generate test data or replicate similar data as in production for your development or test environment?
Visual Studio Database edition provides several features for database development and testing. One of them is generating random test data using Data Generation Plan. Data Generation Plan contains how you want your test data to be generated for your specific tables and columns. It uses several built in data generators which generate random data or generate data from other data sources as per your column data type. You can change properties of these data generators to define the range and format of the data being generated as well. If it does not suffice your need or you have different business rules which can't be satisfied by using the built-in data generators, you can even create your own custom data generators too.
Note: In this demonstration I am going to show how you can generate test data in Visual Studio 2010 Ultimate edition although you can do the same with Visual Studio 2005/2008 Database edition too.
Launch Microsoft Visual Studio 2010 IDE (Integrated Development Studio) and open your database project. Right click on your project node in the Solution Explorer, go to Add and then click on New Item as shown below:
In Add New Item dialog box, select Data Generation Plan node in the left side under Database Project node and choose empty Data Generation Plan template from the detail section. Specify an appropriate name for your data generation plan as shown below and click on the Add button.
The Data Generation Plan will scan through the schema objects of your database project and will list all the tables that exist. By default all the tables are included in the plan for data generation. You can choose which tables you want to generate test data and how many records you want to generate for each table. Depending on the relationship defined the data generation plan shows related tables and you can specify a ratio for related tables for data generation as shown below.
To start generating data, click on the Generate Data icon in the toolbar or hit F5. This will launch the Connect to Database wizard, as shown below, where you need to specify the database details where you want to generate the test data.
The moment you specify database connection and click on the OK button, the data generation plan starts generating data for each table selected in the plan and shows the status as follows. The wizard will also ask if you want to delete data from the target tables before data generation begins inserting rows.
To verify if the data has been generated, connect to your target database and query the record counts for those tables as shown below.
You don't need to generate test data to verify it in your target tables, but rather you can preview the data to be generated beforehand and generate test data only if it meets your requirement (this saves you from creating multiple iterations of test data). Right click on the table in the data generation plan screen and click on Preview Data Generation menu option as shown below and the test data will be shown in a preview screen as shown below.
There are couple of standard and built-in data generators provided (to generate random test data for different data types) which the data generation plan chooses automatically to use depending on the schema of your table or data type of your column. You can change different properties of these data generators associated with each column to define a range or format of data being generated for that column. If this default selection does not suit your requirement, you can change it to use a different data generator than the default. Not only this, you can even create you own custom data generator, to learn more about it click here.
Also note, if you want test data to be generated by using SQL SELECT commands from a data source, you can use the Data Bound Generator.
- Review these additional tips
About the author
View all my tips
Article Last Updated: 2010-12-23