Monday, September 06, 2004

The TestFu Database Populator Framework (DPF presented here) is a framework for generating data for database testing. Given a DataSet, the framework can build a "smart" data generator to you can later use in your databse testing. I will present this at BENUG on sep. 9.

Why this framework ?

When you try to apply unit testing to database, you choices are not much: use transaction - clean up you "mess" after each test or even worse backup and restore the database on each test. In fact, there is an intrisic problem with unit testing of database: you cannot ensure the atomicity of each test, i.e. you cannot leave a database in the state that it was prior to the test (you could by restoring the db  from file at each test, but this is costly).

For example, transaction through enterprise services (see Roy's Rollback attribute) works great... but if you are using IDENTITY columns, then the identity counter is not reseted by the transaction and thus, the test case are correlated.

Now, what if we wanted to build unit tests for database that would not require clean up. This would mean that new data should be generated at each execution, since the previous execution would not have been cleaned up. This is where DPF comes into the picture: the DPF engine will generate new data for each of your unit test execution at no cost.

Generating random data is not difficult

That's true, the System.Random class is easy to use. However, things gets (a bit) more complicated when you generate data for database because you have to ensure that integrity constraint are enforced. This is why you will need such framework.

DPF How-to

The DPF totally relies on DataSet and comes with a few CodeSmith templates to accelerate development. The following how-to could be applied to any of your database.

  1. If not done yet, create the strongly-typed DataSet of your database. You can do this by adding a new Typed DataSet to your project and drag and drop the tables in the designer. (Make sure VS has imported the constraint).
  2. Open the DatabasePopulator.cst template. This template willl create a "strongly typed" database populator for a given database,
     
    where
    • Database will let you choose a database available on your machine, select the target database here,
    • Namespace  is the namespace of the strongly typed generator class,
    • TableNamePrefix is the table prefix in your database (in case there is one). This prefix will be trimmed to create the class names
  3. Open the CrudPopulator.cst template and edit it in order to match the way your DAL access the database. This template will generate populator class that can apply CRUD operation to your database. You can also directly use that template and edit each "throw new NotImplementedException()" statement. (I strongly suggest you edit it the template for your needs).
  4. Open the DatabasePopulatorTest.cst tempate and generate a fixture. This template will generate a fixture that will test each CRUD operation of each table in the database. The data necessary to those tests will be automatically generated by the populator.

 

posted on Tuesday, September 07, 2004 1:38:00 AM UTC  #    Comments [8]
Tracked by:
"bloomindales" (online) [Trackback]
"boys jacking off" (online) [Trackback]
Monday, June 06, 2005 5:35:26 PM UTC
Peli's Blog
Monday, June 06, 2005 5:35:26 PM UTC
ISerializable
Monday, June 06, 2005 5:35:26 PM UTC
I just ran your example. Why is it necessary to hit(open) the database during a test? Shouldn't a mock object be standalone?
A'braham Barakhyahu
Monday, June 06, 2005 5:35:27 PM UTC
Well, the main idea of the framework is to avoid mocking and work on the real database...
Jonathan de Halleux
Monday, June 06, 2005 5:35:27 PM UTC
Either I'm totally confused or there are some broken links somewhere. I can't find the <a title="CodeSmith" href="http://www.ericjsmith.net/codesmith/" target="_blank">CodeSmith</a> templates you mention above. The Database Populator Framework links take me to the BENUG page and the BENUG links referring to Database Populator Framework bring me back here. At some point, I got to a Download page but nothing on the page looked like the Database Populator Framework. So where is the stuff? I found TestFu but the zip file contains a library, not <a title="CodeSmith" href="http://www.ericjsmith.net/codesmith/" target="_blank">CodeSmith</a> templates. There is a DatabasePopulator class in there. Is that what I'm looking for? Does it work with NUnit or just <a title="MbUnit, Generating Unit Testing and Model Based Testing Framework for .NET Framework" href="http://mbunit.tigris.org" target="_blank">MbUnit</a>?
sandman
Monday, June 06, 2005 5:35:27 PM UTC
The material of the presentation is available here:
<br>
<br><a target="_new" href="http://blog.dotnetwiki.org/archive/2004/09/10/941.aspx">http://blog.dotnetwiki.org/archive/2004/09/10/941.aspx</a>
Jonathan de Halleux
Monday, June 06, 2005 5:35:28 PM UTC
Peli's Blog
Monday, June 06, 2005 5:35:28 PM UTC
where can I find a tutorial on the DPF? I guess I've downloaded it in TestFu with testdriven, but I am clueless on how tu use it!
<br>:)
marco
Comments are closed.