首页 > 解决方案 > Azure DevOps Pipeline:无法编译测试

问题描述

我有一个包含 4 个项目的解决方案(为清楚起见简化了名称)

解决方案文件夹包含 *.sln 文件,每个项目一个文件夹(加上 Git 源代码控制的东西)。

三个测试项目各自使用一个私有 NuGet 包“MyNugetPackage”;这个包作为工件存在于我们的 Azure 环境中。他们还使用 NUnit3 - 测试项目的 NuGet 引用如下:

<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" />

我为我们的管道创建了以下 YAML 文件:

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'

最初,这在 MSBuild 步骤中失败了,直到我弄清楚如何设置权限以从我们的 Azure 工件(vstsFeed 属性)中提取它。成功执行 MSBuild 步骤后,执行 DonNetCoreCLI(测试)步骤时失败:我收到多条类似于以下内容的消息:

MyTestFile.cs(13,22):错误 CS0234:命名空间“MyNugetPackage”中不存在类型或命名空间名称“ABC”(您是否缺少程序集引用?) [d:\a\1\s\MyApplication. Test.1\MyApplication.Test.1.csproj]

我假设这意味着测试项目找不到我们的私有“MyNugetPackage”。但是,当 MSBuild 成功时,这怎么可能呢?

我尝试将 MSBuild 步骤换成 Visual Studio 构建,但无济于事(同样,构建步骤有效,测试步骤失败):

- task: VSBuild@1
  inputs:
    solution: '**\*.sln'
    platform: 'any cpu'
    configuration: 'release'
    clean: true
    maximumCpuCount: true
    msbuildArchitecture: 'x64'

然后,我尝试将 MSBuild 步骤换成 .NET Core 构建,使其与测试作业相匹配

- task: DotNetCoreCLI@2
  displayName: Build
  inputs:
    command: build
    projects: '**/*.csproj'
    arguments: '--configuration Release'

现在 BUILD 步骤失败了。

所以在我看来,我有两条前进的路线:

微软的文档看起来很简单(运行你的测试)....希望有人能帮助我走上正确的道路。

标签: azure-devopsmsbuildyamlnunitazure-pipelines

解决方案


所以(最终)对我有用的解决方案是使用.Net Core (DotNetCoreCLI@2) 构建,并将每一步分开,例如

# 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://docs.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'

推荐阅读