You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure DevOps流水线NUnit测试抛异常但仍通过的问题

Azure DevOps流水线中MSTest+NUnit测试出现nunit.framework未找到异常(本地运行正常)

我有一个结合MSTest与NUnit的新项目,目标框架为net4.7.2(受遗留项目限制暂无法升级),使用NUnit 3.13.3和NUnit3TestAdapter 4.2.1。本地通过命令行vstest.console.exe运行测试无任何错误,但提交到Git分支运行Azure DevOps流水线后,测试虽显示通过,但日志中出现System.IO.FileNotFoundException异常,提示无法加载nunit.framework程序集。不想忽略该异常,避免后续引发问题。

YAML配置:

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: |
      **\NetLink.Test.dll
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

流水线日志(翻译后):

2022-09-06T18:32:55.2286691Z 异常
NUnit.Engine.NUnitEngineException: 在D:\a\1\s\src\NetLink.Test\obj\Release\NetLink.Test.dll中执行测试时抛出异常
2022-09-06T18:32:55.2287621Z 加载测试时驱动程序中发生异常。
2022-09-06T18:32:55.2340151Z    在 NUnit.Engine.Runners.DirectTestRunner.LoadDriver(IFrameworkDriver driver, String testFile, TestPackage subPackage)
2022-09-06T18:32:55.2341837Z    在 NUnit.Engine.Runners.DirectTestRunner.LoadPackage()
2022-09-06T18:32:55.2342578Z    在 NUnit.Engine.Runners.DirectTestRunner.EnsurePackageIsLoaded()
2022-09-06T18:32:55.2343323Z    在 NUnit.Engine.Runners.DirectTestRunner.Explore(TestFilter filter)
2022-09-06T18:32:55.2344047Z    在 NUnit.Engine.Runners.MasterTestRunner.Explore(TestFilter filter)
2022-09-06T18:32:55.2345237Z    在 NUnit.VisualStudio.TestAdapter.NUnitEngine.NUnitEngineAdapter.Explore(TestFilter filter)(位于D:\repos\NUnit\nunit3-vs-adapter\src\NUnitTestAdapter\NUnitEngine\NUnitEngineAdapter.cs:第88行)
2022-09-06T18:32:55.2346984Z    在 NUnit.VisualStudio.TestAdapter.NUnit3TestExecutor.RunAssembly(String assemblyPath, IGrouping`2 testCases, TestFilter filter)(位于D:\repos\NUnit\nunit3-vs-adapter\src\NUnitTestAdapter\NUnit3TestExecutor.cs:第275行)
2022-09-06T18:32:55.2349381Z 内部异常: System.IO.FileNotFoundException: 无法加载文件或程序集'nunit.framework, Version=3.13.3.0, Culture=neutral, PublicKeyToken=2638cd05610744eb'或其依赖项之一。系统找不到指定的文件。
2022-09-06T18:32:55.2350958Z 文件名称: 'nunit.framework, Version=3.13.3.0, Culture=neutral, PublicKeyToken=2638cd05610744eb'
2022-09-06T18:32:55.2352646Z    在 System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
2022-09-06T18:32:55.2355042Z    在 System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
2022-09-06T18:32:55.2357386Z    在 System.Activator.CreateInstance(String assemblyString, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo, StackCrawlMark& stackMark)
2022-09-06T18:32:55.2358710Z    在 System.Activator.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo)
2022-09-06T18:32:55.2360814Z    在 System.AppDomain.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes)
2022-09-06T18:32:55.2362021Z    在 System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes)
2022-09-06T18:32:55.2363179Z    在 System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes)
2022-09-06T18:32:55.2364023Z    在 NUnit.Engine.Drivers.NUnit3FrameworkDriver.CreateObject(String typeName, Object[] args)
2022-09-06T18:32:55.2364626Z    在 NUnit.Engine.Drivers.NUnit3FrameworkDriver.Load(String testAssemblyPath, IDictionary`2 settings)
2022-09-06T18:32:55.2365476Z    在 NUnit.Engine.Runners.DirectTestRunner.LoadDriver(IFrameworkDriver driver, String testFile, TestPackage subPackage)
2022-09-06T18:32:55.2365997Z 警告: 程序集绑定日志已关闭。
2022-09-06T18:32:55.2366517Z 要启用程序集绑定失败日志记录,请将注册表值[HKLM\Software\Microsoft\Fusion!EnableLog](DWORD)设置为1。
2022-09-06T18:32:55.2367098Z 注意: 程序集绑定失败日志记录会带来一定性能损耗。
2022-09-06T18:32:55.2367650Z 要关闭此功能,请删除注册表值[HKLM\Software\Microsoft\Fusion!EnableLog]。
2022-09-06T18:32:55.2368120Z NUnit Adapter 4.2.0.0: 测试执行完成

解决方法

1. 确保NUnit.Framework复制到输出目录

检查测试项目中nunit.framework的引用属性,确保复制本地设置为True:

  • 直接编辑测试项目的csproj文件,添加或确认以下配置:
<Reference Include="nunit.framework, Version=3.13.3.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
  <HintPath>..\packages\NUnit.3.13.3\lib\net45\nunit.framework.dll</HintPath>
  <Private>True</Private>
</Reference>

2. 修改VSTest任务指向输出目录

日志显示测试运行的是obj\Release下的DLL,该目录未包含NUnit依赖包。修改VSTest任务的测试集路径,指向bin目录:

- task: VSTest@2
  inputs:
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      **\bin\$(buildConfiguration)\NetLink.Test.dll
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

3. 强制NuGet完整还原

在NuGet还原任务中添加--force参数,确保每次构建都重新下载并还原所有依赖:

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

4. 降级NUnit3TestAdapter版本

NUnit3TestAdapter 4.x对net4.x项目兼容性有限,尝试降级到3.17.0版本(对net4.7.2支持更稳定):

  • 编辑测试项目csproj文件,修改包引用:
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

5. 验证依赖文件是否存在

在VSBuild任务后添加PowerShell步骤,检查输出目录是否包含nunit.framework.dll:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      Get-ChildItem -Path "$(build.sourcesdirectory)\**\bin\$(buildConfiguration)" -Name "nunit.framework.dll" -Recurse

如果未找到,回到步骤1确认复制本地设置是否生效。


内容的提问来源于stack exchange,提问作者greektreat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 23:50:28