带有 NUnit 和 AutoData 的 AutoFixture 抛出 TargetParameterCountException

AutoFixture with NUnit and AutoData throws TargetParameterCountException

在我的单元测试项目中,我安装了 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)

有什么我没有安装或遗漏的吗?

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

原因是AutoFixture.NUnit2 is compiled against version 2.6.2 so if you want to use it with 2.6.4 you'll have to add the following 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版本是测试运行器使用的版本,这可能不同于编译时使用的版本。

因此,虽然您可能正在针对版本 2.6.4, if your test runner uses version 2.6.3, then you need to redirect to 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>