Tech Off Thread

4 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Unit Tests and Databases

Back to Forum: Tech Off
  • User profile image

    I'm fairly new to the concept of unit testing and am looking to start using it in my projects. However my projects are based on a database, so I was wondering how other people write tests against a database. I'm assuming you need a fresh install of a pre-populated database to test against, and that this needs to be installed before every test session.

    Is this the way to do it? How do others write tests against a DB based project?

  • User profile image
    Dr Herbie

    Hi Andy,
        It depends on what you're testing -- your database access code, or the code that uses the database access code.

    Testing database access code I tend to create a stored procedure in the database that will populate a specific table with specific data.  Then I can simply call that stored procedure from the unit test setup code.  I can then remove the data (with another stored procedure) in the teardown.  This guarantees the code will have specific data even if you forgot to copy the database. Smiley

    Testing code that uses the database code may be best done with a mock object that pretends to be the database access code, but always returns static data and/or tracks the calls made.  Google on "Mock Object" for tens of thousands of explanations of this concept.


  • User profile image

    You have options;

    • Mock objects
    • Scripts to setup and tear down the database as part of the test setup and tear down
    • Scripts to attach a clean starting database as part of the build script before testing begings.
    • COM+ transactions which roll back

  • User profile image

    If you want to directly test logic you've written into stored procedures and you're using Microsoft SQL Server, you might want to check out TSQLUnit at SourceForge.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.