Grails : Save time while writing Integration tests

06 / May / 2010 by Amit Jain 2 comments

Hi Friends,

I always used to spend lot of time writing integration tests as running even a single test involves loading the complete application, which includes loading complete database every time. Then I realized that data which I use for testing is always the same, why can’t we restore the database once created, before we run the test so that at least time spent on loading data can be saved. Following is the script which I wrote and worked for me.

mysqldump --user=myusername --password=mypassword databaseName  /home/amit/proj_dev_backup.sql
mysql -u myusername --password=mypassword < /home/amit/updateTestDatabase.sql

cd /home/amit/applicationPath
mv ./grails-app/conf/BootStrap.groovy /home/amit/BootStrap_org.groovy
cp /home/amit/BootStrap.groovy ./grails-app/conf/

grails test-app -integration $@

mv /home/amit/BootStrap_org.groovy ./grails-app/conf/BootStrap.groovy
rm  /home/amit/proj_dev_backup.sql
 

In the above script, databaseName is to be replaced with my development environment database, which is expected to be updated. Here BootStrap.groovy file gets replaced temporarily. If we like, we can also load data in a bootstrap.groovy only for development environment, then we won't need to replace it even temporarily as in the above script.

Following is the updateTestDatabase.sql file used in the above script which creates and updates the test database.

 drop database testDatabaseName; create database testDatabaseName;
 use testDatabaseName;
 source /home/amit/proj_dev_backup.sql
 exit

We also need to update DataSource.groovy file for the test environment as follows:

test {
	dataSource {
             dbCreate = "update" 
             url = "jdbc:mysql://localhost/testDatabaseName"
	     driverClassName = "com.mysql.jdbc.Driver"
	     username = "myusername"
	     password = "mypassword"
       }
}

Now I just run this script, provides the list of files to be tested (if any) as command line arguments, and see the integration test results much faster then ever before.

Hope this helped!

~~Amit Jain~~
amit@intelligrape.com
http://www.tothenew.com/blog/

FOUND THIS USEFUL? SHARE IT

comments (2)

  1. michael kimsal

    Have you considered making an extra environment flag and using a segment in the bootstrap to do your custom stuff, and just leave it all in one bootstrap?

    amitEnv {
    stuff
    }
    if(enviroment==”amitEnv”) {
    // do stuff
    }

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *