azure-devops - Azure DevOps Pipeline:无法编译测试
问题描述
我有一个包含 4 个项目的解决方案(为清楚起见简化了名称)
- 我的应用程序(.Net 标准 2.0)
- MyApplication.Test.1 (.Net Core 3.1)
- MyApplication.Test.2 (.Net Core 3.1)
- MyApplication.Test.3 (.Net Core 3.1)
解决方案文件夹包含 *.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 步骤失败了。
所以在我看来,我有两条前进的路线:
- 要么坚持使用 MSBuild 或 Visual Studio Build,然后弄清楚如何执行我的测试,要么
- 研究如何使用 DotNetcoreCLI@2 而不是使用 MSBuild/Visual Studio Build 成功编译,并希望测试能够按预期工作
微软的文档看起来很简单(运行你的测试)....希望有人能帮助我走上正确的道路。
解决方案
所以(最终)对我有用的解决方案是使用.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'
推荐阅读
- scrapy - 运行具有多个 start_urls 的scrapy spider时缺少导出结果
- jenkins - 如何从 jenkins 轮询最新的产品版本
- mysql - 是否可以在 Lumen/Laravel 中批量插入后将数据添加到相关表中?
- java - 如何在Android Studio中解析没有数组名的Json
- openshift - opm 从图像中修剪目录的所有版本
- java - 如何使用 Java 中的嵌套 for 循环将 2D 数组元素存储在空的 3D 数组中?
- c++ - 如何将基类成员引入派生类定义,但只有一个重载?
- json - 大 JSON 文件的 Angular 材料表中的分页、排序、过滤器失败
- swift - 我正在学习 swift UI 如何制作像图像一样的自定义按钮和自定义文本字段,如图所示
- go - Go 运行时在哪里寻找插件?