dotnet test in GitLab and have it connect to and use containerized services like databases, caches, or in this case Selenium Hub, you can use the following pattern:
- Rely on GitLab CI’s built-in
docker:dind(“docker in docker”) service to run docker commands as job scripts.
docker-composemanually (if you know of a way to somehow use an image instead for this, let me know!)
docker-composeusing a test-specific
docker run [...] mcr.microsoft.com/dotnet/sdk:5.0 dotnet test [...].
Example xUnit Test Class
This is using xUnit, pulling the Selenium Hub URL from
appsettings.json, and grabbing the title of
This is the
appsettings.json that GitLab CI will use. For local development it’s possible to use a separate
appsettings.json pointed to
localhost, since a local
docker-compose setup for development would expose the necessary ports on
localhost. However, GitLab CI creates a new container for each job script, so anything running there will have to connect from the a test container to the Selenium Hub container using the container name that docker provides via it’s magic DNS stuff. (this took me a long time to figure out, and adding a job script of
docker network ls helped.)
Nothing fancy here (this is close to what the Selenium docs show), other than to note what network name (bridged by default) is used (
some-tests) because it will be used later when running
Here’s where the rubber meets the road.
- Alpine now allows
apk add docker-compose(so there’s no need to mess with python/pip installation of
docker-composeis installed, start the Selenium Hub.
- Then run the dotnet tests:
- Notice it runs the
dotnet/sdkimage in the same
some-testsnetwork (so that it can connect to
- Notice it sets the
ASPNETCORE_ENVIRONMENT=CI(so that the test will read from the
- Notice it mounts
-v $PWD:/appso that the output from
--logger "junit"will be readable by the GitLab CI task runner to capture test results. This requires the
JUnitTestLoggernuget package to be installed in the test project(s).
- Notice it calls
docker-compose down, and I’m not entirely sure that’s necessary – I just like to be a good citizen.
- Notice it runs the