Grails : Save time while writing Integration tests
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.sqlIn 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 exitWe 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/
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
}
Are you serious? I don’t agree with your opinion. Tell me what you enjoy about these things?