O'Reilly logo

Perl Testing: A Developer's Notebook by Chromatic, Ian Langworth

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Temporary Databases

Some programs rely on very specific database features. For example, a PostgreSQL or MySQL administration utility needs a deep knowledge of the underlying database. Other programs, including web content management systems, create their own tables and insert configuration data into the databases. Testing such systems with DBD::CSV is inappropriate; you won’t cover enough of the system to be worthwhile.

In such cases, the best way to test your code is to test against a live database—or, at least, a database containing actual data. If you’re already creating database tables and rows with your installer, go a step further and create a test database with the same information.

How do I do that?

Assume that you have an application named My::App (saved as lib/My/App.pm) and a file sql/schema.sql that holds your database schema and some basic data. You want to create both the live and test database tables during the installation process, and you need to know how to connect to the database to do so. One way to do this is to create a custom Module::Build subclass that asks the user for configuration information and installs the database along with the application.

Note

By storing this module in build_ lib/, the normal build process will not install it as it does modules in lib/.

Save the following file to build_lib/MyBuild.pm:

 package MyBuild; use base 'Module::Build'; use DBI; use File::Path; use Data::Dumper; use File::Spec::Functions; sub create_config_file { my $self ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required