6

I have a simple .net 5 project that contains a main project and a unit test project with nUnit3 tests. On my machine (a mac with visual studio for mac fwiw) tests are discovered on build and work as expected.

When I try and set up a build pipeline in Azure dev ops, none of my tests are discovered and I get the follow line in the logs:

Test run detected DLL(s) which were built for different framework and platform versions. Following DLL(s) do not match current settings, which are .NETFramework,Version=v4.0 framework and X86 platform.

GenericRepositoryTests.dll is built for Framework .NETCoreApp,Version=v5.0 and Platform AnyCPU.
Microsoft.TestPlatform.CommunicationUtilities.dll is built for Framework .NETStandard,Version=v2.0 and Platform AnyCPU.

There are more Microsoft dlls it reports but you get the idea. Here is my yaml file for the build process:

# ASP.NET Core (.NET Framework)
# Build and test ASP.NET Core projects targeting the full .NET Framework.
# Add steps that publish symbols, save build artifacts, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/dotnet-core

trigger:
- main

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
# Added this step manually
- task: UseDotNet@2
  displayName: 'Use .NET Core sdk 5.0.100'
  inputs:
    packageType: 'sdk'
    version: '5.0.100'
    includePreviewVersions: true
# Added this step manually
- task: DotNetCoreCLI@2
  displayName: 'dotnet restore'
  inputs:
    command: restore
    projects: '**/*.csproj'
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(build.artifactStagingDirectory)\WebApp.zip" /p:DeployIisAppPath="Default Web Site"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: VSTest@2
  inputs:
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      **\*test*.dll
      !**\*TestAdapter.dll
      !**\obj\**
    searchFolder: '$(System.DefaultWorkingDirectory)'
    codeCoverageEnabled: true

How can I make sure the Azure Dev Ops test runner settings are set up to run .net 5 dlls?

Thanks

youngwt
  • 316
  • 2
  • 7

3 Answers3

13

Thanks to jessehouwing I found that the answer I needed was to use dotnet test rather than VSTest:

Here is my final pipeline:

trigger:
- main

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:

- task: UseDotNet@2
  displayName: 'Use .NET Core sdk 5.0.100'
  inputs:
    packageType: 'sdk'
    version: '5.0.100'
    includePreviewVersions: true

- task: DotNetCoreCLI@2
  displayName: 'dotnet restore'
  inputs:
    command: restore
    projects: '**/*.csproj'

- task: DotNetCoreCLI@2
  displayName: 'dotnet build'
  inputs:
    command: 'build'
    projects: '**/*.sln'

- task: DotNetCoreCLI@2
  displayName: 'dotnet test'
  inputs:
    command: 'test'
    projects: '**/*tests.csproj'
    arguments: '--configuration $(buildConfiguration) --collect "Code coverage"'
    testRunTitle: 'Generic Repository Tests'
youngwt
  • 316
  • 2
  • 7
  • Hi, Thanks for the sharing, you could [Accept it as an Answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235), it could help other community members who get the same issues, thanks. – Edward Han-MSFT Mar 26 '21 at 02:44
  • This does not work if you have to run tests against .DLL (precompiled) – NiKiZe Jun 21 '22 at 17:00
  • and make sure you don't specify `--no-build` in your `dotnet test` task, as building a solution file won't necessarily build the test projects as they may not be referenced by other projects! – thinkOfaNumber Jul 20 '22 at 09:06
2

Please try to install the NUnit3TestAdapter <PackageReference Include="NUnit3TestAdapter" Version="3.17.0" /> and then check the result.

See: TFS Tests do not match framework settings for more workarounds.

Edward Han-MSFT
  • 2,879
  • 1
  • 4
  • 9
  • I was already using that version of the nUnit test adapter when I ran into this problem. Using the dotnet CLI for the test rather than the VSTest runner has fixed the problem for me (as suggested by jessehouwing in the comment of my question) – youngwt Mar 25 '21 at 18:38
  • Gladly to hear that the dotnet test works for this issue and thanks for sharing it here. – Edward Han-MSFT Mar 26 '21 at 02:45
0

.csproj

 <PackageReference Include="coverlet.collector" Version="3.2.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
    </PackageReference>

Pipeline

trigger: none

pr: none

pool:
  vmImage: 'windows-latest'

stages:
 - stage: CreateDeploymentArtifacts
   displayName: Create Deployment Artifacts
   jobs:
    - job: Test
    displayName: Run Xunit test
    steps:
    - task: DotNetCoreCLI@2
      displayName: 'Restore Packages'
      inputs:
        command: 'restore'
        projects: 'LegalRegTech.Web.sln'
    - task: DotNetCoreCLI@2
      displayName: 'Build application'
      inputs:
        command: 'build'
        projects: 'LegalRegTech.Web.sln'
        arguments: '--no-restore'
    - task: DotNetCoreCLI@2
      displayName: 'Run tests'
      inputs:
        command: 'test'
        projects: '**/*tests.csproj'
        arguments: '--collect:"XPlat Code Coverage"'
    - script: 'dotnet tool install -g dotnet-reportgenerator-globaltool'  
      displayName: 'Install ReportGenerator tool'
    - script: 'reportgenerator -reports:$(Agent.TempDirectory)/**/coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)/coverlet/reports -reporttypes:"Cobertura"'
      displayName: 'Create reports'
    - task: PublishCodeCoverageResults@1  
      displayName: 'Publish code coverage'  
      inputs:  
        codeCoverageTool: Cobertura  
        summaryFileLocation: '$(Build.SourcesDirectory)/coverlet/reports/Cobertura.xml' 
San Jaisy
  • 15,327
  • 34
  • 171
  • 290