Symfony unit tests with Redis on GitLab

_garybell profile image Gary Bell Updated on ・2 min read

I've written a few times about GitLab and the CI/CD pipeline features it has. For most cases, it's straightforward to use. But, every so often, things don't always work as easily as the documentation makes out.

A project I'm working on uses Symfony components, and has elements which use Redis to aid performance. They work great, that's not the issue. Unit testing is, however. Especially on GitLab.

GitLab has documentation on how to use Redis with the CI pipeline, but it is a general document to point in the right direction. It's not possible to cover all languages and implementations which may be required by people. Which is where I came unstuck.

The main thing to be aware of is that using the Redis service is the best way of doing this. Trying to install redis on the docker image let to "could not connect" errors and more failures.

Symfony's documentation for the Redis adapter is pretty good. The combination of the documentation there and Gitlab's documentation for Redis give the total solution.

Gitlab say to use the host for Redis as redis. Symfony has the Redis URLs documented as being redis://host:port for the most part, except for this little nugget:

Alt Text

The port isn't always required. It's also worth noting that Symfony fails to connect to Redis unless the host is prefixed redis://. Combine the two documents and the host URL for unit testing Redis components in Gitlab is simply:

Took a bit of trial and error, but I finally got there. Unit tests are now passing, and I'm hoping I don't have to make any other changes to the CI for a while now. Just back to coding.

This article was originally posted on my blog at https://www.garybell.co.uk/symfony-unit-tests-with-redis-on-gitlab/ on 5th February 2020

Posted on by:

_garybell profile

Gary Bell


A Senior Developer working mostly with PHP and JavaScript, with a bit of Python thrown in for good measure, all on Linux


markdown guide