1

So I have an ASP.NET project in a folder (src) and a test project in a folder right next to the other folder (tests). What I am trying to achieve is to be able to run my tests and deploy the application using docker, however I am really stuck.

Right now there is a Dockerfile in the src folder, which builds the application and deploys it just fine. There is also a Dockerfile for the test project in the tests folder, which should just run my tests.

The tests/Dockerfile currently looks like this:

FROM microsoft/dotnet:2.2.103-sdk AS build
WORKDIR /tests
COPY ["tests.csproj", "Tests/"]
RUN dotnet restore "Tests/tests.csproj"
WORKDIR /tests/Tests
COPY . .
RUN dotnet test

But if i run docker build, the tests fail, I am guessing because the application's code to be tested is missing. I am getting a lot of:

The type or namespace name 'MyService' could not be found (are you missing a using directive or an assembly reference?

I do have a projectreference in my .csproj file so what could be the problem?

J. Loe
  • 201
  • 5
  • 14

1 Answers1

0

Your test code references some files (containing the type MyService) that have not been copied to the image. This happens because your COPY . . instruction is executed after the WORKDIR /tests/Tests instruction, therefore you are copying everything inside the /tests/Tests folder, and not the referenced code which, according to your description, resides in the src folder.

Your problem should be solved by performing COPY . . in your second line, right after the FROM instruction. That way, all the required files will be correctly copied to the image. If you proceed this way, you can simplify your Dockerfile to something like this (not tested):

FROM microsoft/dotnet:2.2.103-sdk AS build
COPY . .                       # Copy all files
WORKDIR /tests/Tests           # Go to tests directory
ENTRYPOINT ["dotnet", "test"]  # Run tests (this will perform a restore + build before launching the tests)
Tao Gómez Gil
  • 2,228
  • 20
  • 36
  • This did not solve the problem I am guessing because the Dockerfile is under the `/tests` folder. The `src` folder is next to the `tests` folder so the `COPY . .` command still won't copy the referenced code. – J. Loe May 03 '19 at 17:45
  • Yes, but you can solve this problem running the `docker run` command from the parent directory, and specifying your `Dockerfile` location with the `-f` option, as shown in this answer: https://stackoverflow.com/a/34300129/2651069 – Tao Gómez Gil May 04 '19 at 12:13