My integration tests have dependency on redis. They have dockerfile included, so everything works in CI. However when running locally from test explorer I have to manually start redis container.
Is there possibility for IDE to automatically run/debug tests in docker containers, so when attemptying to run/debug test in IDE (or maybe even using dotnet test) everything runs in docker?
Please note, that I want to run/debug tests in docker, not docker in tests.
3
Answers
Seems like there is no solution to do so right now.
Here is ticket for rider: https://youtrack.jetbrains.com/issue/RIDER-38942 and for visual studio: https://developercommunity.visualstudio.com/t/allow-running-unit-tests-in-docker/554907
There are some hacks, which you can do to attach debugger to test project, but all of them require some manual interaction and experience is not as seamless as running/debugging the application.
Why not run the container manually?
Such a way you will get a working environment after the tests (all containers will be running).
It’s possible to do this using the remote testing feature introduced in Visual Studio 17.5.
For example, let’s say my solution looks like this, and I want to run
MyLogic.Tests
in a Linux environment via Docker Desktop.testenvironments.json:
Note if your environment is uncomplicated, you can just reference the docker image directly instead of a
dockerFile
, such as:"dockerImage": "mcr.microsoft.com/dotnet/sdk:7.0"
In my case, I have an extra dependancy to put into the image, so my
Dockerfile
looks like so:You should see in Test explorer that your remote environment is now available for selection:
If all is well, Test Explorer should then work pretty much the same as when running directly on the host environment in terms of run and debug.