skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.


  2. Why not run the container manually?

    1. Install docker to the local machine.
    2. Use Docker.DotNet nuget in your test project to work with the docker api.
    3. Stop all containers.
    4. Start the containers you need from your code manually.
    5. Run the tests and clear the state after every one.

    Such a way you will get a working environment after the tests (all containers will be running).

    Login or Signup to reply.
  3. 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.

    MyLogic.sln
       QuantLib
          NQuantLib.dll
          NQuantLib.so.0
          NQuantLibc.so
       testenvironments.json
       MyLogic
          MyLogic.csproj
       MyLogic.Tests
          MyLogic.Tests.csproj
          Dockerfile
    

    testenvironments.json:

    {
      "version": "1", // value must be 1
      "environments": [
        {
          "name": "Linux x64",
          "type": "docker",
          "dockerFile": "MyLogic.Tests\Dockerfile"
        }
      ]
    }
    

    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:

    FROM mcr.microsoft.com/dotnet/sdk:7.0 AS base
    COPY ["QuantLib/", "/usr/lib/"]
    RUN ldconfig
    

    You should see in Test explorer that your remote environment is now available for selection:

    Visual Studio Remote Environment 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search