无法运行NUnit测试。

4
我安装了NUnit 2.6.1,试图在Windows 7 x64上运行一个简单的测试。这导致了一个异常。
“尝试加载格式不正确的程序。您可能正尝试将使用CLR的较新版本构建的程序集加载到NUnit当前运行的版本(2.0.50727)之下,或者尝试将64位程序集加载到32位进程中。”
这相当奇怪,因为nunit.exe.config看起来像下面这样。
  <?xml version="1.0" encoding="utf-8" ?> 
- <configuration>
- <!-- 
   The GUI only runs under .NET 2.0 or higher. The
   useLegacyV2RuntimeActivationPolicy setting only
   applies under .NET 4.0 and permits use of mixed 
   mode assemblies, which would otherwise not load 
   correctly.


  --> 
- <startup useLegacyV2RuntimeActivationPolicy="true">
- <!--  Comment out the next line to force use of .NET 4.0 
  --> 
- <!--  <supportedRuntime version="v2.0.50727" /> 
  --> 
  <supportedRuntime version="v4.0.30319" /> 
  </startup>
- <runtime>
- <!--  Ensure that test exceptions don't crash NUnit 
  --> 
  <legacyUnhandledExceptionPolicy enabled="1" /> 
- <!--  Run partial trust V2 assemblies in full trust under .NET 4.0 
  --> 
  <loadFromRemoteSources enabled="true" /> 
- <!--  Look for addins in the addins directory for now 
  --> 
- <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <probing privatePath="lib;addins" /> 
  </assemblyBinding>
  </runtime>
  </configuration>
1个回答

7
这似乎不是NUnit的问题。这似乎是您的单元测试程序集未构建为32位进程。您确定您的单元测试程序集是为32位构建的吗?如果NUnit正在运行32位,而您的程序集是64位(或未构建为任何CPU),则会出现此问题。调用应用程序确定所需程序集的位深度。您不能在32位进程中使用64位dll文件,反之亦然。
我提到这个唯一的原因是因为您的问题说您正在尝试运行测试。如果NUnit配置不正确,它甚至无法启动。

不明白你的意思。NUnit 是 64 位的,对吧?我的系统也是 64 位的,我需要测试的程序也是 64 位的。问题出在哪里呢? - Alexandre
2
问题在于您正在构建64位dll,但以32位模式运行NUnit。您确定您正在运行64位的NUnit吗?64位有单独的可执行文件。或者您正在反其道而行之,运行64位的NUnit并加载32位的dll。无论哪种方式,都存在不匹配的情况,导致程序出现错误。 - devshorts
不,我不是。如何在64位模式下运行它(NUnit)?<<或者你正在另一种方式下进行,你正在运行64位的NUnit并加载32位的dll文件。>>实际上,我不知道。我怎么知道呢? - Alexandre
1
从Nunit网站上得知:“64位平台上的NUnitnunit.exe的.NET 2.0版本是使用/platform:anycpu构建的,这会导致在32位系统上jit编译为32位代码,在64位系统上jit编译为64位代码。当NUnit用于测试64位系统上的32位应用程序时,会出现异常。为避免此问题,请在64位系统上测试32位代码时使用使用/platform:x86构建的nunit-x86.exe。” - devshorts
现在它正在工作。然而,尽管我有64位Windows,但我无法创建64位应用程序。项目->属性->生成中的Dropdownlist仅具有一个值,出于某种原因是“Active(x86)”。为什么? - Alexandre
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接