Extract unit test results from multi-stage Docker build (.NET Core 2.0)

前端 未结 1 1094
谎友^
谎友^ 2021-01-31 19:10

I am building a .NET Core 2.0 web API and I am creating a Docker image. I am quite new to Docker so apologies if the question has been answered before.

I have the follow

相关标签:
1条回答
  • 2021-01-31 19:28

    Thanks for your question - I needed to solve the same thing.

    I added a separate container stage based on results of the build. The tests and its output are all handled in there so they never reach the final container. So build-env is used to build and then an intermediate test container is based on that build-env image and final is based on runtime container with results of build-env copied in.

    # ---- Test ----
    # run tests and capture results for later use. This use the results of the build stage
    FROM build AS test
    #Use label so we can later obtain this container from the multi-stage build
    LABEL test=true
    WORKDIR /
    #Store test results in a file that we will later extract 
    RUN dotnet test --results-directory ../../TestResults/ --logger "trx;LogFileName=test_results.xml" "./src/ProjectNameTests/ProjectNameTests.csproj"
    

    I added a shell script as a next step that then tags the image as project-test.

    #!bin/bash
    id=`docker images --filter "label=test=true"  -q`
    docker tag $id projectname-test:latest
    

    After that, I basically do what you do which is use docker cp and get the file out. The difference is my test results were never in the final image so I don't touch the final image.

    Overall I think the correct way to handle tests is probably create a test image (based on the build image) and run it with a mounted volume for test results and have it run the unit tests when that container starts. Having a proper image/container would also allow you to run integration tests etc. This article is older but details similar https://blogs.infosupport.com/build-deploy-test-aspnetcore-docker-linux-tfs2015/

    0 讨论(0)
提交回复
热议问题