Using SQL Data Generator with your Unit Tests

Last month we released SQL Data Generator which is a great tool for generating your devtest data. One of the decisions we made for 1.0 was not to include an API. I wanted to be able to incorporate the data generation into my automated tests, so I have created an extension to SQL Data Generator as a side project which allows you to incorporate the generation process into your unit (well Integration) tests. You can now generate data as part of your unit tests.

I have wrote an article on my approach and how to use the framework over at Simple Talk –

For the extension, I took two approaches. One approach was just a POCO (Plain Old CLR Object) which would execute the command line application for a given project file. This could be placed anywhere in your code, ideally I was thinking in the TestFixtureSetup.

public void TestFixtureSetup()
    SDGConsoleWrapper sdgConsoleWrapper = new SDGConsoleWrapper(@”DatabaseNorthwindEmployees_Orders.sqlgen”);

As a parameter, you pass in the filename for the project file created via the UI.

The second approach is using custom attributes. By adding an attribute ‘SDG’ to the TestFixture and inheriting from ‘SDGContext’, we hooked into the calls for each method.  At the top of your test method, you can add the ‘SDGExecute’ with the project file name, this will then be executed before that test started.

public class DataAccessTests : SDGContext
   public void GetCustomerByID_SDGProject_1000Rows()
      DataTable dt = DataAccess.GetCustomerByID(“00040”);

      Assert.AreEqual(“00040”, dt.Rows[0][“CustomerID”]);
      Assert.AreEqual(“Suppebentor Holdings “, dt.Rows[0][“CompanyName”]);
      Assert.AreEqual(“Bobbi Yates”, dt.Rows[0][“ContactName”]);
      Assert.AreEqual(“Web”, dt.Rows[0][“ContactTitle”]);
      Assert.AreEqual(“564 White Old Freeway”, dt.Rows[0][“Address”]);

Best of all – this works with all of the unit testing frameworks! NUnit, MbUnit, XUnit, MSTest! They all can take advantage.  Feel free to download (from our CodePlex project) and start using it, I suggest you read the article for more of an in-depth overview of what is going on and the possible ways you can take advantage of it.

Finally, we would love to hear your feedback on this. I created this in my free time and is not a supported solution. However, do you think the application should have a fully supported API? Are you happy with the approach I have taken or do you think there are better ways of doing this?  Maybe your comments will help shape SQL Data Generator 2.0.

Article Link:

Application Download Link: Red Gate SQL Data Generator

Download and Source:

Leave a Reply

Your email address will not be published. Required fields are marked *