【问题标题】:AutoFixture with NUnit and AutoData throws TargetParameterCountException带有 NUnit 和 AutoData 的 AutoFixture 抛出 TargetParameterCountException
【发布时间】:2016-02-24 10:24:25
【问题描述】:

在我的单元测试项目中,我安装了 AutoFixture (v3.40.0)、NUnit (v2.6.4.) 和 AutoFixtrue.NUnit2(v3.39.0)。
我在其中一个虚拟测试用例上使用 AutoData 属性

[Test, AutoData]
public void IntroductoryTest(
    int expectedNumber)
{               

}

,但是在运行测试时我得到了

System.Reflection.TargetParameterCountException : Parameter count mismatch.
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at NUnit.Core.Reflect.InvokeMethod(MethodInfo method, Object fixture, Object[] args)
   at NUnit.Core.TestMethod.RunTestMethod()
   at NUnit.Core.TestMethod.RunTestCase(TestResult testResult)

有什么我没有安装或缺少的东西吗?

【问题讨论】:

  • 你需要 both NUnit 和 NUnit2 吗?这可能会导致该问题。
  • FWIW,NUnit 2 的插件模型导致各种问题,所以如果你有任何选择,请考虑使用 xUnit.net,其中 AutoFixture.XUnit2 i> 和 AutoFixture.Xunit 多年来一直完美无缺。

标签: c# unit-testing nunit autofixture


【解决方案1】:

该异常是由于 NUnit 没有加载 AutoFixture add-in在运行时导致的,因此测试参数没有得到任何参数。

原因是AutoFixture.NUnit2 是针对版本2.6.2 编译的,因此如果您想将它与2.6.4 一起使用,您必须将以下assembly binding redirects 添加到您的测试项目的配置文件中:

<configuration>
    <runtime>
        <dependentAssembly>
            <assemblyIdentity
                name="nunit.core.interfaces" 
                publicKeyToken="96d09a1eb7f44a77"
                culture="neutral" />
            <bindingRedirect
                oldVersion="0.0.0.0-2.6.4.14350"
                newVersion="2.6.4.14350" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity
                name="nunit.core"
                publicKeyToken="96d09a1eb7f44a77"
                culture="neutral" />
            <bindingRedirect
                oldVersion="0.0.0.0-2.6.4.14350"
                newVersion="2.6.4.14350" />
          </dependentAssembly>
    </runtime>
</configuration>

请注意,您需要重定向到的 NUnit 版本是 test runner 使用的版本,它可能与编译期间使用的版本不同。 p>

因此,虽然您可能正在针对版本 2.6.4 编译测试,但如果您的测试运行程序使用版本 2.6.3,那么您需要重定向到 2.6.3

<configuration>
    <runtime>
        <dependentAssembly>
            <assemblyIdentity
                name="nunit.core.interfaces" 
                publicKeyToken="96d09a1eb7f44a77"
                culture="neutral" />
            <bindingRedirect
                oldVersion="0.0.0.0-2.6.3.13283"
                newVersion="2.6.3.13283" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity
                name="nunit.core"
                publicKeyToken="96d09a1eb7f44a77"
                culture="neutral" />
            <bindingRedirect
                oldVersion="0.0.0.0-2.6.3.13283"
                newVersion="2.6.3.13283" />
          </dependentAssembly>
    </runtime>
</configuration>

【讨论】:

  • 我刚刚在 App.config 中添加了&lt;dependentAssembly&gt; &lt;assemblyIdentity name="nunit.core.interfaces" publicKeyToken="96d09a1eb7f44a77" culture="neutral" /&gt; &lt;bindingRedirect oldVersion="0.0.0.0-2.6.3.13283" newVersion="2.6.3.13283" /&gt; &lt;/dependentAssembly&gt;,这就足够了。您可以编辑您的答案,我会将其标记为已接受。
  • 绑定重定向适用于 NUnit 2.6.3。在您提到 NUnit 2.6.4 的问题中。你用的是哪个版本?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-04
  • 1970-01-01
相关资源
最近更新 更多