Search code examples
c#.netunit-testingazure-devopsyaml

Azure DevOps Pipeline: Building and Testing Separate .NET Projects in Same Solution


In azure DevOps I have an organisation with 2 .NET projects: "MyProject" and "MyProject.Tests". Both projects belong to the same solution. I am building a pipepeline, and the YAML file is located in "MyProject". The below code snippet from this YAML file is supposed to build "MyProject" and then run tests from "MyProject.Tests"

jobs:
- job: Build and Test
  displayName: Build and Test
  steps:
  - task: DotNetCoreCLI@2
    inputs:
      command: 'build'
      projects: '**/MyProject.csproj'
  - task: DotNetCoreCLI@2
    inputs:
      command: 'test'
      projects: '**/MyProject.Tests.csproj'

The build task (DotNetCoreCLI@2 with build command) works fine for "MyProject.csproj", but the test task (DotNetCoreCLI@2 with test command) fails to find "MyProject.Tests.csproj" (I know that '**/MyProject.Tests.csproj' is an incorrect path). How can I modify my YAML file to correctly reference and run the unit tests from "MyProject.Tests.csproj" after building "MyProject.csproj"?

Additional Details:

  • Both projects (MyProject and MyProject.Tests) are part of the same solution.
  • I'm using NUnit for unit testing in MyProject.Tests.
  • The pipeline YAML file is located in the root directory of MyProject.
  • The goal is to build MyProject first and then run unit tests from MyProject.Tests using Azure DevOps pipeline. Any insights or corrections on how to structure the YAML file or reference the test project correctly would be greatly appreciated!

"MyProject" file structure

MyProject.Tests" file structure


Solution

  • In Azure DevOps Pipeline, all project files will be checked out to the source folder: $(Build.sourcesdirectory) by default.

    To make sure that the MyProject.Tests.csproj can be found correctly, you can set the following path to the dotnet test task. projects: '$(build.sourcesdirectory)/**/MyProject.Tests.csproj'

    For example:

    jobs:
    - job: Build and Test
      displayName: Build and Test
      steps:
      - task: DotNetCoreCLI@2
        inputs:
          command: 'build'
          projects: '**/MyProject.csproj'
      - task: DotNetCoreCLI@2
        inputs:
          command: 'test'
          projects: '$(build.sourcesdirectory)/**/MyProject.Tests.csproj'
    

    It will go through all the files in the source folder to find matching files.

    Update:

    From your screenshots about the project files structure, the MyProject and MyProject.tests are from two repos.

    To use the files in the Pipeline, you need to add the repo resources in Azure Pipeline.

    Then you need to checkout two repos in Azure Pipeline. Then you will find the related csproj files.

    For example:

    resources:
      repositories:
      - repository: MyProjectTests 
        type: git
        name: YourProjectName/MyProject.Tests 
    
    
    
    jobs:
    - job: Build and Test
      displayName: Build and Test
      steps:
      - checkout: self
      - checkout: MyProjectTests
      - task: DotNetCoreCLI@2
        inputs:
          command: 'build'
          projects: '$(build.sourcesdirectory)/**/MyProject.csproj'
      - task: DotNetCoreCLI@2
        inputs:
          command: 'test'
          projects: '$(build.sourcesdirectory)/**/MyProject.Tests.csproj'