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

关于Nunit3-Console无法运行x86架构.NET6.0测试DLL的问询

问题描述

此前使用Nunit3-console.exe v3.15运行测试无异常,近期因部分测试需加载32位非托管DLL,将测试项目(NunitTests.dll)从AnyCpu改为x86架构,该测试DLL基于.NET6.0框架。

执行测试命令:

nunit3-console.exe c:\Source\PSTS5\NunitTests\bin\Debug\net6.0-windows\NunitTests.dll --testlist=d:\PSTS\TestsToRun.txt

出现错误:

NUnit.Engine.NUnitEngineException : Failed to load the test assembly c:\Source\PSTS5\NunitTests\bin\Debug\net6.0-windows\NunitTests.dll
  ----> System.IO.FileLoadException : Could not load file or assembly 'NunitTests, Version=1.0.0.1, Culture=neutral, PublicKeyToken=null'.
--NUnitEngineException
Failed to load the test assembly c:\Source\PSTS5\NunitTests\bin\Debug\net6.0-windows\NunitTests.dll
   at NUnit.Engine.Drivers.NUnitNetCore31Driver.Load(String assemblyPath, IDictionary`2 settings) in C:\projects\nunit-console\src\NUnitEngine\nunit.engine.core\Drivers\NUnitNetCore31Driver.cs:line 77
   at NUnit.Engine.Runners.DirectTestRunner.LoadDriver(IFrameworkDriver driver, String testFile, TestPackage subPackage) in C:\projects\nunit-console\src\NUnitEngine\nunit.engine.core\Runners\DirectTestRunner.cs:line 136
   at NUnit.Engine.Runners.DirectTestRunner.LoadPackage() in C:\projects\nunit-console\src\NUnitEngine\nunit.engine.core\Runners\DirectTestRunner.cs:line 126
   at NUnit.Engine.Runners.DirectTestRunner.EnsurePackageIsLoaded() in C:\projects\nunit-console\src\NUnitEngine\nunit.engine.core\Runners\DirectTestRunner.cs:line 235
   at NUnit.Engine.Runners.DirectTestRunner.RunTests(ITestEventListener listener, TestFilter filter) in C:\projects\nunit-console\src\NUnitEngine\nunit.engine.core\Runners\DirectTestRunner.cs:line 182
   at NUnit.Engine.Runners.AbstractTestRunner.Run(ITestEventListener listener, TestFilter filter) in C:\projects\nunit-console\src\NUnitEngine\nunit.engine.core\Runners\AbstractTestRunner.cs:line 168
   at NUnit.Engine.Runners.MasterTestRunner.RunTests(ITestEventListener listener, TestFilter filter) in C:\projects\nunit-console\src\NUnitEngine\nunit.engine\Runners\MasterTestRunner.cs:line 472
   at NUnit.Engine.Runners.MasterTestRunner.Run(ITestEventListener listener, TestFilter filter) in C:\projects\nunit-console\src\NUnitEngine\nunit.engine\Runners\MasterTestRunner.cs:line 150
   at NUnit.ConsoleRunner.ConsoleRunner.RunTests(TestPackage package, TestFilter filter) in C:\projects\nunit-console\src\NUnitConsole\nunit3-console\ConsoleRunner.cs:line 197
--
FileLoadException
Could not load file or assembly 'NunitTests, Version=1.0.0.1, Culture=neutral, PublicKeyToken=null'.
   at System.Runtime.Loader.AssemblyLoadContext.LoadFromPath(IntPtr ptrNativeAssemblyLoadContext, String ilPath, String niPath, ObjectHandleOnStack retAssembly)
   at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath)
   at NUnit.Engine.Drivers.NUnitNetCore31Driver.Load(String assemblyPath, IDictionary`2 settings) in C:\projects\nunit-console\src\NUnitEngine\nunit.engine.core\Drivers\NUnitNetCore31Driver.cs:line 71

尝试添加--x86参数时,提示:

The x86 option is not available on this platform.

可在Visual Studio 2022中正常运行这些测试,咨询两个问题:

  1. 是否可以使用Nunit3-console运行x86架构的.NET6.0测试DLL?
  2. 除Nunit3-console外,还有其他运行这些测试的方式吗?
解决方案

问题1:能否用Nunit3-console运行x86架构的.NET6.0测试DLL?

可以,但需要调整启动方式:

  • NUnit3-Console的--x86参数仅对.NET Framework测试有效,.NET 5+测试需要依托对应架构的.NET运行时启动。
  • 确保已安装x86版本的.NET 6运行时。
  • 使用x86版的dotnet.exe启动NUnit3-Console,示例命令:
    # 直接调用NUnit3-Console的dll文件
    C:\Program Files (x86)\dotnet\dotnet.exe C:\路径\到\nunit3-console.dll c:\Source\PSTS5\NunitTests\bin\Debug\net6.0-windows\NunitTests.dll --testlist=d:\PSTS\TestsToRun.txt
    
    # 若NUnit3-Console已全局安装,可简化为
    C:\Program Files (x86)\dotnet\dotnet.exe nunit3-console c:\Source\PSTS5\NunitTests\bin\Debug\net6.0-windows\NunitTests.dll --testlist=d:\PSTS\TestsToRun.txt
    

问题2:其他运行测试的方式

1. dotnet test命令

这是.NET 5+原生的测试运行命令,直接指定x86架构即可:

# 通过项目文件运行
dotnet test c:\Source\PSTS5\NunitTests\NunitTests.csproj --arch x86 --test-list d:\PSTS\TestsToRun.txt

# 直接运行已编译的DLL
dotnet test c:\Source\PSTS5\NunitTests\bin\Debug\net6.0-windows\NunitTests.dll --arch x86 --test-list d:\PSTS\TestsToRun.txt

2. vstest.console.exe(Visual Studio测试命令行工具)

使用x86版本的vstest.console.exe,路径随Visual Studio版本和安装位置略有不同,示例:

"C:\Program Files (x86)\Microsoft Visual Studio\2022\Community\Common7\IDE\Extensions\TestPlatform\vstest.console.exe" c:\Source\PSTS5\NunitTests\bin\Debug\net6.0-windows\NunitTests.dll /TestList:d:\PSTS\TestsToRun.txt /Platform:x86

3. NUnit Visual Studio Test Adapter

配合VS的测试适配器,可直接复用VS中能正常运行测试的配置,通过vstest.console.exe调用即可(同上述第2种方式)。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 17:40:17