c# - Nant 和 Nunit2 的问题

标签 c# nunit nant

我在尝试使用 nunit2 任务执行测试时遇到此错误

Failure executing test(s). If you assembly is not built using NUnit version 2.6.0.12051, then ensure you have redirected assembly bindings

我的测试项目的 nunit 版本是 2.6.2.12296。

我在我的测试项目配置文件上测试了几个重定向绑定(bind),但没有任何效果。我知道我可以使用 EXEC 直接运行 nunit.exe 而不是使用 nunit2 任务,但我想让它工作。

更新

这是我当前用于测试项目的 app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
    </dependentAssembly>
    </assemblyBinding>

    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
          <assemblyIdentity name="nunit.framework" publicKeyToken="96d0234a77" culture="Neutral" />
          <bindingRedirect oldVersion="0.0.0.0-2.6.0.12051" newVersion="2.6.2.12296" />
        </dependentAssembly>
    </assemblyBinding>

  </runtime>
</configuration>

更新 2

这是构建文件的相关部分

<nunit2>
  <test 
    assemblyname="D:\[the full path]\UnitTests.dll" 
    appconfig="D:\customTest.config"/>
  <formatter type="Plain"/>
</nunit2>

最佳答案

当您使用 NAnt NUnit2 任务时,如果您的测试框架版本与构建 NAnt 的版本不匹配,您必须告诉 NAnt NUnit2 任务使用绑定(bind)重定向。

问题是 NUnit 测试运行器会为每个测试创建一个新的 AppDomain,因此您无法使用测试项目的 app.config 进行绑定(bind)重定向。

您必须使用绑定(bind)重定向创建自定义配置文件:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="nunit.framework" publicKeyToken="96d0234a77" culture="Neutral" />
      <bindingRedirect oldVersion="0.0.0.0-2.6.2.12051" newVersion="2.6.2.12296" />
    </dependentAssembly>
  </assemblyBinding>
</runtime> 

并告诉 NUnit2 NAnt 任务使用它:

<nunit2>
    <test assemblyname="ProjectName.Tests.dll" appconfig="customTest.config" />
     ...
</nunit2>

------------最后的想法------------------------ ------

你的配置是:

<bindingRedirect oldVersion="0.0.0.0-2.6.0.12051" newVersion="2.6.2.12296" />

试试

<bindingRedirect oldVersion="0.0.0.0-2.6.2.12296" newVersion="2.6.2.12296" />

并仔细检查您的测试项目的所有外部引用是否在 visualstudio 属性窗口中 copy local=true

关于c# - Nant 和 Nunit2 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19802825/

相关文章:

.net - 是否有显示所有属性名称/值的 NAnt 任务?

c# - 预加载 .NET 程序集的好方法

c# - 如何从 jQuery ajax 调用将复杂对象传递给 ASP.NET WebApi GET?

c# - 在 lambda 表达式中声明文字日期

c# - 如何从已部署的 Windows 窗体应用程序运行 NUnit

c# - 如何替换已弃用的 csc ant 任务

nant - 使用Nant创建目录

c# - 网络开发新手。 ASP.NET 还是 Django?

javascript - 突出显示 jQuery 日期选择器中的特定日期

c# - 使用最小起订量模拟第三方回调事件