使用NUnit和AutoData的AutoFixture会抛出TargetParameterCountException

dra*_*vic 5 c# nunit unit-testing autofixture

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

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

}
Run Code Online (Sandbox Code Playgroud)

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

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)
Run Code Online (Sandbox Code Playgroud)

有什么我没有安装或我失踪?

Enr*_*lio 4

该异常是由 NUnit在运行时未加载 AutoFixture插件引起的 导致的,因此测试参数没有获取任何参数。

原因是AutoFixture.NUnit2是针对版本2.6.2编译的,因此如果您想在2.6.4中使用它,您必须将以下程序集绑定重定向添加到测试项目的配置文件中:

<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>
Run Code Online (Sandbox Code Playgroud)

请注意,您需要重定向到的 NUnit 版本是测试运行程序使用的版本,它可能与编译期间使用的版本不同。

因此,虽然您可能针对版本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>
Run Code Online (Sandbox Code Playgroud)