Search code examples
.netcontinuous-integrationgitlab-ciartifactdotnet-build

How to use artifacts for dotnet test


I would like to use the artifacts from my build stage for the test stage. But somehow the dotnet test command does not find any tests and instantly exits.

The dotnet restore & dotnet build command seem to work differently depending on whether executed locally or in the CI.

My .gitlab-ci:

image: mcr.microsoft.com/dotnet/sdk:7.0

variables:
  CONFIGURATION: 'Release'
  MSBUILDDISABLENODEREUSE: "1"
  TEST_RESULT_DIRECTORY: '.test-results'

default:
  tags:
    - .NET 6.0
    - WPF

stages:
  - Build
  - Test

Build:
  stage: Build
  script:
    - dotnet restore --no-cache
    - dotnet build --no-restore --configuration $CONFIGURATION
  artifacts:
    paths:
      - ./**/bin/Release
      - ./**/obj/Release
    expire_in: 1 day

Test:
  stage: Test
  needs:
    - job: Build
      artifacts: true
  script:
    - dotnet test --no-build --nologo --configuration $CONFIGURATION --test-adapter-path:. --logger:"junit;LogFilePath=../$TEST_RESULT_DIRECTORY/{assembly}.xml;MethodFormat=Class;FailureBodyFormat=Verbose"
  artifacts:
    when: always
    reports:
      junit: 
        - ./$TEST_RESULT_DIRECTORY/*.xml

Maybe I'm just missing a critical file in the artifacts, but by comparing the local and CI generated files I noticed just a difference in additional NuGet related files on local generation.


Solution

  • After some digging I found out that not all files which are needed for dotnet test are put in the obj/Release folder. Some of them are one level above.

    The solution is to include the full obj directory.

      artifacts:
        paths:
          - ./**/bin/Release
          - ./**/obj