Just another developer blog

Loading test data with ScalaTest + Play

Loading test data with ScalaTest + Play

The ScalaTest + Play library provides a couple of useful traits for when your ScalaTest Play Framework functional tests need a running application for context. The OneAppPerSuite trait will share the same Application instance across all tests in a class whereas the OneAppPerTest trait gives each test its own Application instance.

These traits will ensure you have a running application for your tests but if you want to test code that operates on a database it can be helpful to load some known test data before each test and clean it up afterwards. For this, we can mix in a ScalaTest before-and-after trait.

The BeforeAndAfter trait lets you define a piece of code to run before each test with before and/or after each test with after. There is also a BeforeAndAfterAll trait which invokes methods before and after executing the suite but resetting the database for each test makes for better test isolation.

Here is a base test class that sets up a running application and uses the Evolutions companion object to load and clean up the database. Note this class uses Guice dependency injection to retrieve the database object but you can also easily connect to a database using the Database companion object.

We also override the db.default.url config value to point to a test database.

You can also load evolutions from the file system if you don’t want to define them in the code.



1 thought on “Loading test data with ScalaTest + Play”

  • Hi David,

    thanks for sharing this, put me in the right direction with a testing-problem. I think, the Scala-community could profit a lot from those small, but complete (with imports and all) snippets and examples!

Leave a Reply

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