Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Tools

Ensuring Database Quality


Writing Database Tests

There's no magic when it comes to writing database tests, you write them just like you would any other type of test. A database test is typically a three-step process:

  • Setup the test. Each individual unit test should be run against predefined test data. Furthermore, your database should be in a known state before running your database test suite. There are three common strategies for doing this. First, start fresh by dropping the existing schema then rebuilding it from scratch reloading the initial test data. This is a brute-force approach that is straightforward, safe, but a bit slow. Second, keep the existing schema but replace initial test data. This is a bit faster than the first approach because it avoids recreation of the schema, but runs the risk of data corruption if you don't ensure that all data is replaced. Third, restore the database from a backed up image.
  • Run the test. You want to automate your tests so that you can run them whenever you need to. Modern approaches to development such as Extreme Programming (XP) and the Rational Unified Process (RUP) are predicated upon the concept that you will evolve your system over time to meet the changing needs of your stakeholders. If you want to ensure that your system works, the implication is that you need good regression testing tools. Unfortunately, database testing tools are few and far between, although some tools such as Quest Unit Tester for Oracle (www.unit-test.com), TSQLUnit for MS SQL Server (tsqlunit.sourceforge .net), and IBM Rational Functional Tester (www-306.ibm.com/software/awdtools/ tester/ functional/) are available.
  • Check the results. Each individual test should determine whether it was successful and report the result back to you. When an error occurs, you need to be able to determine what happened by browsing the database or by doing a table dump from within your program.

A common debate amongst developers is where to obtain test data: Should you use production data or create your own test data? The answer is that you need both. For unit testing, I prefer to create sample data to ensure that I can predict the actual results for each test. For other forms of testing, particularly system integration testing and function testing, I will use production data so as to better simulate real-world conditions. For load/stress testing, I will use production data if it is available; otherwise, I will create the requisite test data. Tools such as DBUnit (www.dbunit.org) and DTM Data Generator (www.sqledit.com/dg/) are good options for creating test data.

There are several strategies for managing test data, each of which can be used alone or in combination:

  • First, you can maintain an external definition of the test data, perhaps in flat files, XML files, or a secondary set of tables. This data would be loaded in from the external source as needed.
  • Second, you could have test data creation scripts, written using data manipulation language (DML) SQL code or application source code (Java or C#, for instance), which does the necessary deletions, insertions, and/or updates required to create the test data.
  • Third, you could have self-contained test cases that put the database into the known state required for the specific test.

A significant advantage of the second and third strategies is that it is much more likely that the developers of that code will place it under configuration management (CM) control. Although it is possible to put test data itself under CM control, worst case you generate an export file that you check in. This isn't a common practice and therefore may not occur as frequently as required. My advice is to choose an approach that reflects the culture of your organization.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.