Search code examples
c#asp.nettestingyamlazure-pipelines

Passing .NET build to separate test job in Azure Pipeline (YAML) - Tests not found/executed


I have an Azure Pipeline with 2 jobs.
The first job builds my solution and creates an artifact to later be used for deployment. The second job builds the solution again and then executes the tests.

Up until now, both jobs were running parallel, since they both were building. To save time and resources, I would like to use the build from the first job in the second one. As I can't use the artifact that is created, since that one does not contain test assemblies, I changed the pipeline as follows:

  1. The first job copies the build as a separate artifact (additional to the published one)
  2. The second job downloads the build artifact, copies it to the source directory

This way, in my opinion, I should be able to run the tests as before, but without having to build in advance. The folder structure with the new way is identical to the old one with custom building.

However, this seems to not be working. The tests are found (or at least dotnet test does not fail with an error not finding the assemblies), but no tests are executed and I get a warning at the end:

##[warning]No test result files were found.

This is my YAML file:

jobs:
- job: Build
  displayName: Build backend
  pool:
    vmImage: 'ubuntu-latest'
  steps:
  - task: Cache@2
    displayName: NuGet - Cache packages
    inputs:
      key: 'nuget | "$(Agent.OS)" | backend/**/*.csproj,!backend/**/bin/**'
      restoreKeys: |
        nuget | "$(Agent.OS)"
      path: $(Pipeline.Workspace)/.nuget/packages

  - task: UseDotNet@2
    displayName: .NET - Acquire version in global.json
    inputs:
      packageType: 'sdk'
      useGlobalJson: true
      includePreviewVersions: false

  - task: DotNetCoreCLI@2
    displayName: .NET - Restore Tools
    inputs:
      command: 'custom'
      custom: 'tool'
      arguments: 'restore'

  - task: DotNetCoreCLI@2
    displayName: .NET - Restore NuGet packages
    inputs:
      command: restore
      nugetConfigPath: 'backend/nuget.config'
      projects: 'backend/*.sln'

  - task: DotNetCoreCLI@2
    displayName: .NET - Build solution
    continueOnError: false
    inputs:
      command: build
      arguments: '--configuration Release -warnaserror -p:Version=1.0.0.$(Build.BuildId)'
      projects: 'backend/*.sln'

  - task: DotNetCoreCLI@2
    displayName: .NET - Publish solution
    inputs:
      command: 'publish'
      publishWebProjects: false
      projects: 'backend/**/MyWebProject.csproj'
      arguments: '--no-build --configuration Release --output $(Build.ArtifactStagingDirectory)/publish'
      zipAfterPublish: true

  - task: PublishBuildArtifacts@1
    displayName: Publish solution
    inputs:
      PathtoPublish: '$(Build.ArtifactStagingDirectory)/publish'
      ArtifactName: '${{parameters.artifactNameBackend}}'
      publishLocation: 'Container'

  - task: CopyFiles@2
    displayName: 'Copy build for testing'
    inputs:
      contents: 'backend/**/bin/**'
      targetFolder: '$(Build.ArtifactStagingDirectory)/testing-build'

  - publish: '$(Build.ArtifactStagingDirectory)/testing-build'
    displayName: 'Publish build for testing'
    artifact: TestingBuild

- job: Test
  displayName: Test solution
  dependsOn: Build
  condition: succeeded()
  pool:
    vmImage: 'ubuntu-latest'
  steps:
  - download: current
    displayName: Download build for testing
    artifact: TestingBuild

  - task: CopyFiles@2
    displayName: 'Copy build to source directory'
    inputs:
      sourceFolder: '$(Pipeline.Workspace)/TestingBuild'
      targetFolder: '$(Build.SourcesDirectory)'

  - task: CmdLine@2
    inputs:
      script: |
        echo "Structure of work folder of this pipeline:"
        tree $(Pipeline.Workspace)

  - task: UseDotNet@2
    displayName: .NET - Acquire version in global.json
    inputs:
      packageType: 'sdk'
      useGlobalJson: true
      includePreviewVersions: false

  - task: DotNetCoreCLI@2
    displayName: .NET - Restore Tools
    inputs:
      command: 'custom'
      custom: 'tool'
      arguments: 'restore'

  - task: DotNetCoreCLI@2
    displayName: .NET - Test solution
    inputs:
      command: 'test'
      projects: '**/*Test/*.csproj'
      arguments: '--no-build --configuration Release'

Here's the output of the file tree. I added that to check whether the directories are correct. (I stripped everything non-relevant) https://file.io/EiD5mz816xa9

And here is the output of the testing task:

2024-05-17T09:43:15.0989550Z ##[section]Starting: .NET - Test solution
2024-05-17T09:43:15.0996119Z ==============================================================================
2024-05-17T09:43:15.0996239Z Task         : .NET Core
2024-05-17T09:43:15.0996300Z Description  : Build, test, package, or publish a dotnet application, or run a custom dotnet command
2024-05-17T09:43:15.0996414Z Version      : 2.238.1
2024-05-17T09:43:15.0996620Z Author       : Microsoft Corporation
2024-05-17T09:43:15.0996691Z Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/build/dotnet-core-cli
2024-05-17T09:43:15.0996805Z ==============================================================================
2024-05-17T09:43:15.4203778Z Info: .NET Core SDK/runtime 2.2 and 3.0 are now End of Life(EOL) and have been removed from all hosted agents. If you're using these SDK/runtimes on hosted agents, kindly upgrade to newer versions which are not EOL, or else use UseDotNet task to install the required version.
2024-05-17T09:43:16.9859317Z [command]/opt/hostedtoolcache/dotnet/dotnet test /home/vsts/work/1/s/backend/test/MyWebProject.Test.IntegrationTest/MyWebProject.Test.IntegrationTest.csproj --logger trx --results-directory /home/vsts/work/_temp --no-build --configuration Release
2024-05-17T09:43:25.0115556Z 
2024-05-17T09:43:25.2160615Z ##[warning]No test result files were found.
2024-05-17T09:43:25.2168654Z Info: Azure Pipelines hosted agents have been updated and now contain .Net 5.x SDK/Runtime along with the older .Net Core version which are currently lts. Unless you have locked down a SDK version for your project(s), 5.x SDK might be picked up which might have breaking behavior as compared to previous versions. You can learn more about the breaking changes here: https://docs.microsoft.com/en-us/dotnet/core/tools/ and https://docs.microsoft.com/en-us/dotnet/core/compatibility/ . To learn about more such changes and troubleshoot, refer here: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/build/dotnet-core-cli?view=azure-devops#troubleshooting
2024-05-17T09:43:25.2170489Z ##[section]Finishing: .NET - Test solution

I suppose, that it comes down to a dumb mistake maybe in a path or something similar. But I just can't find it...


Solution

  • I checked the file you shared, and I found in your second job, you didn't add the dotnet task to restore NuGet packages. This may result in missing files required by the dotnet test --no-build command.

    I also tested in my local to copy the bin folder from a build to a clean solution in another folder. If I directly run the dotnet test --no-build command, the test will not run. If I run the dotnet restore before dotnet test --no-build command, the test is successful.

    In this case, you can add the dotnet restore task before dotnet test task.

      - task: DotNetCoreCLI@2
        displayName: .NET - Restore NuGet packages
        inputs:
          command: restore
          nugetConfigPath: 'backend/nuget.config'
          projects: 'backend/*.sln'