如何在Azure管道中构建假程序集

问题描述 投票:0回答:1

简介:

我有一个 C# 项目,也有相关的单元测试项目。它使用微软的单元测试框架及其技术来创建假货。它在本地运行良好 - 我能够构建项目并运行单元测试。

问题:

现在我尝试在 dev.azure.com 上添加管道,但收到如下警告:

Primary reference "___.Fakes". ##[warning]C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets(2352,5): Warning MSB3245: Could not resolve this reference. Could not locate the assembly "___.Fakes". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.

以及类似错误:

##[error]___\___Tests\___Tests.cs(9,43): Error CS0234: The type or namespace name 'Fakes' does not exist in the namespace 'Microsoft.AspNetCore.SignalR.Client' (are you missing an assembly reference?)

等等

我已经做了什么:

  • 我将
    Microsoft.VisualStudio.TestPlatform.TestFramework.dll
    Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll
    作为解决方案项,并手动浏览引用它,因为这些 dll 在 NuGET 中不可用。
  • 我已关闭 MSBuild Arguments 中的 IntelliTrace
    /p:EnableIntelliTrace=false

问题

  • 如何让这条管道发挥作用?那么如何在 Azure 上的管道构建过程中创建 Fakes 程序集?
c# azure unit-testing pipeline microsoft-fakes
1个回答
0
投票

不要显式引用某些 DLL,而是尝试使用 NuGet 包。这有助于保证在构建过程中正确解决所有必需的依赖关系。您表示手动引用

"Microsoft.VisualStudio.TestPlatform.TestFramework.dll"
"Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll."
并在本地构建项目,然后将其推送到 Azure DevOps 存储库中。在大多数情况下,这些应该可以通过 NuGet 包获得。

确保检查您的 csproj 文件并且所有引用都已正确添加到其中:-

enter image description here

确保参考此单元测试运行教程以在 Azure DevOps 管道中成功运行单元测试:-

trigger:
- master

pool:
  vmImage: 'windows-latest'

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

steps:
- 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:PackageLocation="$(build.artifactStagingDirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: VSTest@2
  inputs:
    testSelector: 'testAssemblies'
    testAssemblyVer2: '$(System.DefaultWorkingDirectory)'
- task: VSTest@2
  inputs:
    testSelector: 'testAssemblies'
    testAssemblyVer2: '$(System.DefaultWorkingDirectory)/<test-files-dlls>'
    searchFolder: '$(System.DefaultWorkingDirectory)'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: DotNetCoreCLI@2
  displayName: 'Run unit tests - $(buildConfiguration)'
  inputs:
    command: 'test'
    projects: '**/*.csproj'
    arguments: '--no-build --configuration $(buildConfiguration)'

输出:-

enter image description here

在 Azure 测试计划选项卡中测试运行成功,如下所示:-

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.