Azure DevOps Pipeline: can't get tests to compile - azure-devops

I have a solution containing 4 projects (names simplified for clarity)
MyApplication (.Net Standard 2.0)
MyApplication.Test.1 (.Net Core 3.1)
MyApplication.Test.2 (.Net Core 3.1)
MyApplication.Test.3 (.Net Core 3.1)
The solution folder contains the *.sln file, and one folder per project (plus stuff for Git source control).
The three test projects each consume a private NuGet package, "MyNugetPackage"; this package exists on our Azure environment as an artifact. They also use NUnit3 - the test project's NuGet references are as follows:
<PackageReference Include="MyNugetPackage" Version="2020.1.1" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
I created the following YAML file for our pipeline:
trigger:
- master
pool:
vmImage: 'windows-latest'
variables:
buildConfiguration: release
platform: x64
steps:
- task: NuGetToolInstaller#1
inputs:
versionSpec:
checkLatest: true
- task: NuGetCommand#2
inputs:
command: 'restore'
restoreSolution: '**/*.sln'
feedsToUse: 'select'
vstsFeed: '{GuidA}/{GuidB}'
- task: MSBuild#1
inputs:
solution: '**/*.sln'
msbuildArchitecture: 'x64'
platform: 'Any CPU'
configuration: 'Release'
msbuildArguments: '-m'
clean: true
- task: DotNetCoreCLI#2
inputs:
command: test
projects: '**/*Tests/*.csproj'
arguments: '--configuration Release'
Initially, this failed at the MSBuild step, until I worked out how to set up the permissions for it to pull from our Azure artifact (the vstsFeed property). Having got the MSBuild step to execute successfully, it then fails when the DonNetCoreCLI (Test) step executes: I get multiple messages similar to:
MyTestFile.cs(13,22): error CS0234: The type or namespace name 'ABC'
does not exist in the namespace 'MyNugetPackage' (are you missing an
assembly reference?)
[d:\a\1\s\MyApplication.Test.1\MyApplication.Test.1.csproj]
I'm assuming that this means the Test projects can't find our private 'MyNugetPackage'. But how could this be when MSBuild succeeded?
I tried swapping the MSBuild step out for a Visual Studio build, but to no avail (again, build step worked, test step failed):
- task: VSBuild#1
inputs:
solution: '**\*.sln'
platform: 'any cpu'
configuration: 'release'
clean: true
maximumCpuCount: true
msbuildArchitecture: 'x64'
I then tried swapping the MSBuild step for the .NET Core build so that it matched the job for the tests
- task: DotNetCoreCLI#2
displayName: Build
inputs:
command: build
projects: '**/*.csproj'
arguments: '--configuration Release'
And now the BUILD step failed.
So it seems to me that I have two routes forward:
Either stick with either MSBuild or Visual Studio Build, and work out how to then execute my tests, or
Work out how to compile successfully with DotNetcoreCLI#2 rather than use MSBuild/Visual Studio Build, and hope that the tests will then work as expected
The documentation from Microsoft seemed so simple (Run your tests)....hoping someone can help guide me on the right path.

You can run dotnet test command on your local machine to check if it works fine locally. Make sure you can successfully build and test your projects locally first.
In you pipeline, You can try use vstest task after msbuild task to run the tests.
- task: VSTest#2
inputs:
testAssemblyVer2: |
**\*[Tt]est*.dll
!**\*[Tt]estAdapter*.dll
!**\obj\**
searchFolder: $(system.defaultworkingdirectory)
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
You can also run Dotnet restore task to restore the packages before dotnet test task
- task: DotNetCoreCLI#2
displayName: 'dotnet restore'
inputs:
command: restore
projects: '**/*.csproj'
feedsToUse: select
vstsFeed: 'nugettest'

So the solution that (eventually) worked for me was to use the .Net Core (DotNetCoreCLI#2) build, and to break every step apart, e.g.
# ASP.NET Core
# Build and test ASP.NET Core projects targeting .NET Core.
# Add steps that run tests, create a NuGet package, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/dotnet-core
trigger:
- master
pool:
vmImage: 'windows-latest'
variables:
buildConfiguration: 'Release'
platform: x64
steps:
- task: DotNetCoreCLI#2
displayName: 'Restore for MyApplication'
inputs:
command: 'restore'
projects: 'MyApplication/*.csproj'
feedsToUse: 'select'
vstsFeed: '{GuidA}/{GuidB}'
- task: DotNetCoreCLI#2
displayName: 'Restore for MyApplication.Test.1'
inputs:
command: 'restore'
projects: 'MyApplication.Test.1/*.csproj'
feedsToUse: 'select'
vstsFeed: '{GuidA}/{GuidB}'
- task: DotNetCoreCLI#2
displayName: 'Restore for MyApplication.Test.2'
inputs:
command: 'restore'
projects: 'MyApplication.Test.2/*.csproj'
feedsToUse: 'select'
vstsFeed: '{GuidA}/{GuidB}'
- task: DotNetCoreCLI#2
displayName: 'Restore for MyApplication.Test.3'
inputs:
command: 'restore'
projects: 'MyApplication.Test.3/*.csproj'
feedsToUse: 'select'
vstsFeed: '{GuidA}/{GuidB}'
- task: DotNetCoreCLI#2
displayName: 'Build MyApplication'
inputs:
command: 'build'
projects: 'MyApplication/*.csproj'
- task: DotNetCoreCLI#2
displayName: 'Build Test.1'
inputs:
command: 'build'
projects: 'MyApplication.Test.1/*.csproj'
- task: DotNetCoreCLI#2
displayName: 'Build Test.2'
inputs:
command: 'build'
projects: 'MyApplication.Test.2/*.csproj'
- task: DotNetCoreCLI#2
displayName: 'Build Test.3'
inputs:
command: 'build'
projects: 'MyApplication.Test.3/*.csproj'
- task: DotNetCoreCLI#2
displayName: 'Execute Test.1s'
inputs:
command: 'test'
projects: 'MyApplication.Test.1/*.csproj'
- task: DotNetCoreCLI#2
displayName: 'Execute Test.2s'
inputs:
command: 'test'
projects: 'MyApplication.Test.2/*.csproj'
- task: DotNetCoreCLI#2
displayName: 'Execute Test.3s'
inputs:
command: 'test'
projects: 'MyApplication.Test.3/*.csproj'

Related

Selectively publish projects as Nuget packages to Azure Devops Artifacts

We have a solution with several projects. We only want 2 projects of them to be published to artifacts. Ideally we can manage this by changing settings in the project, hence the pipeline can be generic. We have been searching (trial/error) for a possible solution either on the pipeline side, either on the projects side. None of them worked or no projects ended up in the artifacts or all. Any suggestions ?
#Build and distribute nnn.Core NuGets to nnnNugets artifacts feed
name: $(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r)
variables:
feedName : 'nnnNugets'
buildConfiguration: 'debug'
trigger:
- nugetify
pool:
vmImage: windows-latest
steps:
- task: DotNetCoreCLI#2
displayName: 'Build'
inputs:
command: 'build'
projects: '**/*.csproj'
arguments: '--configuration $(buildConfiguration)'
- task: DotNetCoreCLI#2
displayName: 'Pack'
inputs:
command: pack
versioningScheme: byBuildNumber
- task: NuGetAuthenticate#1
displayName: 'NuGet Authenticate'
- task: NuGetCommand#2
displayName: 'NuGet push'
inputs:
command: push
publishVstsFeed: '$(feedName)'
allowPackageConflicts: true
- task: PublishSymbols#2
inputs:
searchPattern: '**/bin/**/*.pdb'
publishSymbols: true
symbolServerType: 'teamServices'
SymbolExpirationInDays: 1000
IndexableFileFormats: Pdb
The solution was to update the PackagesToPack propery so that the selections matches the projects you want.include
- task: DotNetCoreCLI#2
displayName: 'Pack All'
inputs:
command: pack
packagesToPack: '**/*.csproj; !**/Debug/**.csproj'
versioningScheme: byBuildNumber

Azure DevOps pipeline release Error: No package found with specified pattern:

I have an ASP.NET Core project in Azure DevOps repository and it gets built OK using the DevOps build pipeline. However, the release over that builds always fails with this error:
No package found with specified pattern.Check if the package mentioned in the task is published as an artifact in the build or a previous stage and downloaded in the current job.
More Details:
# ASP.NET Core
# Build and test ASP.NET Core projects targeting .NET Core.
# Add steps that run tests, create a NuGet package, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/dotnet-core
trigger:
branches:
include:
- release/*
pool:
vmImage: 'windows-2022'
variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
projectName: '**/F11.Web.csproj'
runtime: 'win-x64'
steps:
- task: UseDotNet#2
displayName: 'Use .NET 5 SDK (preview)'
inputs:
packageType: 'sdk'
version: '5.0.100-rc.1.20452.10'
vsVersion: '16.8.0'
includePreviewVersions: true
- task: DotNetCoreCLI#2
inputs:
command: 'restore'
projects: '$(projectName)'
feedsToUse: 'select'
- task: DotNetCoreCLI#2
displayName: Build
inputs:
projects: '$(projectName)'
arguments: '--no-restore'
- task: DotNetCoreCLI#2
displayName: Test
inputs:
command: test
projects: '$(projectName)'
arguments: '-l "console;verbosity=detailed"'
- task: DotNetCoreCLI#2
displayName: 'Publish WebApi'
inputs:
command: publish
publishWebProjects: false
projects: '$(projectName)'
arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory) --runtime -r $(runtime)'
- task: CopyFiles#2
inputs:
Contents: '**'
TargetFolder: '$(Build.ArtifactStagingDirectory)'
- task: PublishPipelineArtifact#1
inputs:
targetPath: '$(Pipeline.Workspace)'
artifactName: 'PublishBuildArtifact'
Azure DevOps pipeline release Error: No package found with specified pattern:
First, you could make sure you have generated the artifact for your build pipeline, check it from the build history in the build pipeline.
Second, when you using the release pipeline to deploy the artifact, you need make sure you have select the correct build pipeline.
Third, set the correct configuration for the task IIS web app deploy:
If above not help you, please share the configuration for the task IIS web app deploy with image in your question.

Azure Dev Ops test step not finding tests (.net 5)

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
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'
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.
.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'

Can I mix framework projects and .net standard projects in the same repository? Dotnet Pack command

In the answer to this question I needed to change the build yaml from using Nuget to using DotNet
So that the .net standard project would pack correctly ( i hope ). However when I did this I started getting
error MSB4057: The target "pack" does not exist in the project
for the framework projects.
Should I be separating the repositories or is there some way to specify different pack commands for different targets?
My original azure-pipelines.yml was
# .NET Desktop
# Build and run tests for .NET Desktop or Windows classic desktop solutions.
# Add steps that publish symbols, save build artifacts, and more:
# https://learn.microsoft.com/azure/devops/pipelines/apps/windows/dot-net
trigger:
- master
pool:
vmImage: 'windows-latest'
variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
Major: '2'
Minor: '0'
Patch: '0'
steps:
- task: NuGetToolInstaller#0
inputs:
versionSpec: '>=4.3.0'
checkLatest: true
- task: NuGetCommand#2
inputs:
restoreSolution: '$(solution)'
- task: VSBuild#1
inputs:
solution: '$(solution)'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
- task: NuGetCommand#2
inputs:
command: pack
packagesToPack: '**/*.csproj'
versioningScheme: byPrereleaseNumber
majorVersion: '$(Major)'
minorVersion: '$(Minor)'
patchVersion: '$(Patch)'
- task: NuGetCommand#2
inputs:
command: pack
packagesToPack: '**/*.vbproj'
versioningScheme: byPrereleaseNumber
majorVersion: '$(Major)'
minorVersion: '$(Minor)'
patchVersion: '$(Patch)'
- task: NuGetCommand#2
displayName: 'NuGet push'
inputs:
command: push
publishVstsFeed: 'SBDCommonFeed'
allowPackageConflicts: true
but I changed the NuGetCommand#2 task to be
- task: DotNetCoreCLI#2
inputs:
command: 'pack'
outputDir: '$(Build.ArtifactStagingDirectory)/TestDir'
For the Nuget command I can experiment with PackagesToPack
What do I change for the DotNet command?
[Update]
I tried
- task: DotNetCoreCLI#2
inputs:
command: 'pack'
projects: '**/*Standard.csproj'
outputDir: '$(Build.ArtifactStagingDirectory)/OutputDir'
Where *Standard are the .net standard projects. However there was still the MSB4057 error on a framework project
The error indicates you're trying to pack both the .net standard project and .net framework project while you only need to pack the .net standard library project. (Also, dotnet pack should not be used to pack one non-library project.)
So the workaround is to specify the project to pack when using dotnet pack task. The Pack command in .net core task also supports packagesToPack element.
See #packagesToPack: '**/*.csproj' # Required when command == Pack.
Normally the format is:
steps:
- task: DotNetCoreCLI#2
displayName: 'dotnet pack'
inputs:
command: pack
packagesToPack: '**/ProjectName.csproj'
configuration: Release
versioningScheme: xxx
minorVersion: xxx
buildProperties: xxx
...

Azure devops .netcore fails but builds fine in visual studio

--Fixed it... Geez this stuff is Janky.
Microsoft link to explain what to do
https://learn.microsoft.com/en-us/aspnet/core/migration/21-to-22?view=aspnetcore-2.2&tabs=visual-studio
I had to remove the version tag from the Microsoft.AspNetCore.All references in the csproj file.
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" **DELETE Version Version="2.2.0" />
--Edit 12/4
I I installed on my PC .netcore to 2.2, Now it fails the same. I didn't change a setting in my Project. I am specifying in my project settings to use 2.1 not 2.2.
It appears in the YAML where i set the .netcore version is not applying.
- task: DotNetCoreInstaller#0
displayName: 'Use .NET Core Tool Installer'
inputs:
version: 2.1.500
I am not sure what to do now.
--
I get following error when building in azure devops or publish from VS to an Azure app service. Visual Studio builds it fine.
(Restore target) -> 2018-12-04T01:30:44.4900171Z
D:\a\1\s\cbw.services\cbw.mvc.services.csproj : error NU1202: Package
Microsoft.AspNetCore.All 2.2.0 is not compatible with netcoreapp2.1
(.NETCoreApp,Version=v2.1). Package Microsoft.AspNetCore.All 2.2.0
supports: netcoreapp2.2 (.NETCoreApp,Version=v2.2)
My Build YAML
resources:
- repo: self
queue:
name: Hosted VS2017
steps:
- task: DotNetCoreInstaller#0
displayName: 'Use .NET Core Tool Installer'
inputs:
version: 2.1.500
- task: NuGetToolInstaller#0
displayName: 'Use NuGet 4.9.1'
inputs:
versionSpec: 4.9.1
#- task: NuGetCommand#2
# displayName: 'NuGet restore'
# inputs:
# restoreSolution: '***.sln'
# feedsToUse: config
# nugetConfigPath: 'NuGet.config'
# externalFeedCredentials: <Name of the NuGet service connection>
- task: DotNetCoreCLI#2
displayName: Restore
inputs:
command: restore
projects: '**/*.sln'
feedsToUse: config
nugetConfigPath: NuGet.config
- task: DotNetCoreCLI#2
displayName: Build
inputs:
projects: '**/*.sln'
arguments: '--configuration $(BuildConfiguration)'
- task: DotNetCoreCLI#2
displayName: Test
inputs:
command: test
projects: '**/*Tests/*.csproj)'
arguments: '--configuration $(BuildConfiguration)'
#- task: DotNetCoreCLI#2
# displayName: Publish
# inputs:
# command: publish
# publishWebProjects: True
# arguments: 'release --output $(build.artifactstagingdirectory)'
- task: DotNetCoreCLI#2
displayName: 'dotnet publish'
inputs:
command: 'publish'
publishWebProjects: True
projects: '**/*.sln'
arguments: release -o $(build.artifactStagingDirectory)
zipAfterPublish: True
modifyOutputPath: false
- task: PublishBuildArtifacts#1
displayName: 'Publish Artifact'
inputs:
PathtoPublish: '$(build.artifactstagingdirectory)'
- task: AzureRmWebAppDeployment#3
inputs:
azureSubscription: 'Azure CBW Dev'
WebAppName: 'cbwServicesDev'
Package: $(System.artifactstagingdirectory)/**/*.zip
You need to add the .NET Core SDK installer task to your build.
Edit: It needs to be the first task, or at least before any build task.
A good post by Scott Hanselman walks you through this problem.