Chapter 6. Testing Databases

Many programs need to work with external data. Given Perl’s powerful and useful modules for database access, many programs use relational databases, simple flat files, and everything in between. It’s in those places, where the real world and your program interact, that you need the most tests.

Fortunately, the same testing tools and techniques used elsewhere make testing databases and database access possible. The labs in this chapter explore some of the scenarios that you may encounter with applications that rely on external data storage and provide ideas and solutions to make them testable and reliable.

Shipping Test Databases

Many modern applications store data in databases for reasons of security, abstraction, and maintainability. This is often good programming, but it presents another challenge for testing; anything outside of the application itself is harder to test. How do you know how to connect to the database? How do you know which database the user will use?

Fortunately, Perl’s DBI module, a few testing tools, and a little cleverness make it possible to be confident that your code does what it should do both inside the database and out.

Often, it’s enough to run the tests against a very simple database full of testable data. DBI works with several database driver modules that are small and easy to use, including DBD::CSV and DBD::AnyData. The driver and DBI work together to provide the same interface that you’d have with a fully relational database ...

Get Perl Testing: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.