Tuesday, July 12, 2011

Data driven functional testing using Perl and MySQL

So I stumbled upon this conversation, I don't remember how, but the question was asked about a "quick sample", if there is such a thing, of using a database to store test data for functional tests. Specifically this question was asked in a forum of Selenium users.

I find that Perl has a good set of modules for, well for EVERYTHING! literally! Including interfacing with databases. It is very well documented by the way. Check out http://dbi.perl.org/ for complete details as well as online docs with loads of reference material.

But basically in order to get data to be used in the test scripts you can do the following (again, this is in Perl and I'm using DBI with the msql driver):

1. Define the test data by writing the sql queries needed to get the data from the db
2. Create a connection to a database
3. Execute the query and stuff the data into an array
4. I then iterate through the data in the array until there is no more data.
5. Cleanup

In Perl here is an example:

use DBI;

my $query = "SELECT email, pword, open_url, provider_name
             FROM messaging_test_data_tbl 
             WHERE is_active = true 
             LIMIT 4";

my $dbh = DBI->connect("DBI:$dbtype:$dbname",$dbun,$dbpw,
            { RaiseError => 1, PrintError => 1, AutoCommit => 1 });

my $sth = $dbh->prepare($query);

### GET THE TEST DATA FROM THE DB
$sth->execute()                       
          or die "Cannot execute the query: $sth->errstr\n";

### AND THEN ITERATE USING fetchrow_array ###
while ($email,$pword,$open_url,$provider_name) = $sth->fetchrow_array())
{

 do all of your test stuff with the first set of data, second,
 within this while loop

}
 $dbh->disconnect;
 exit;


Basically, no matter what language you are using you can use a similar flow. And don't forget to check out the linked and referenced documentation. Its a good reference; no matter what language you are programming your tests in.

1 comment:

  1. Hello Freddy,
    The Article on Data driven functional testing using Perl and MySQL is informative.. It gives detailed information about it. Thanks for Sharing the information about the Functional testing For More information check the detail on Functional testing check,Software Testing Company

    ReplyDelete

Creative Commons License
VGP-Miami Web and Mobile Automation Blog by Alfred Vega is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.