Tips for performing faster tests with Symfony

In a previous post we discussed how to make good data sets for testing purposes. The method described is a good way of testing an application. How can we perform faster tests though?

If you have to do a lot of tests for your app, it can take a long time for those tests to complete. Seriously. I’m not talking about a “check your messages while they run” wait or even a “go and make yourself a cup of tea while they run” wait, more like a “go to the kitchen for tea, get into an in-depth discussion with a co-worker whilst waiting for the kettle to boil, realise the tea bags are finished so pop down to the shop for some fresh ones, boil the kettle again and finally make yourself a cup of tea while they run” wait.

The point is, tests can be slow, we need to find a way to speed them up.

What can we do to make faster tests?

When we take a closer look at the test process, we see that fixtures are loaded for each individual test. Loading fixtures individually in this way is one thing which takes up a lot of time during the testing process. If we could load the fixtures just once for all the tests, we could save a lot of time. But if we could load all the fixtures in one go, how well would our tests run and would they modify the data we have entered? Let’s have a go and find out…

Make lots of copies

This first configuration shows that we are using SQLite and that we have created a lot of copies of the database.

We use Default_db_path when we make an update to the database schema or when we are running the first test. Creating a separate database for each test allows us to use the same data many times and thus saves time when running the tests. (remember to run the console command for updating fixtures storage when the database schema is updated).

This command makes the test database from the fixtures we added and saves it with a snapshot path.

This creates a bootstrap file for PhpUnit.

So, in this way our tests have been made significantly faster.