[英]AutoFixture with NUnit and AutoData throws TargetParameterCountException
In my unit test project I have installed AutoFixture (v3.40.0), NUnit (v2.6.4.) and AutoFixtrue.NUnit2(v3.39.0). 在我的单元测试项目中,我安装了AutoFixture(v3.40.0),NUnit(v2.6.4。)和AutoFixtrue.NUnit2(v3.39.0)。
I'm using AutoData attribute on one of the dummy test cases 我在其中一个虚拟测试用例上使用AutoData属性
[Test, AutoData]
public void IntroductoryTest(
int expectedNumber)
{
}
, but when running the test I get the ,但是在运行测试时,我得到了
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)
Is there anything I haven't installed or I'm missing? 有什么我没有安装或我失踪?
That exception is caused by NUnit not loading the AutoFixture add-in at runtime so the test parameters don't get any arguments. 该异常是由NUnit 在运行时未加载AutoFixture 加载项引起的,因此测试参数不会获得任何参数。
The reason is that 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 to the configuration file of your test project: 原因是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>
Note that the version of NUnit you need to redirect to is the one used by the test runner , which could be different than the version used during compilation. 请注意,您需要重定向到的NUnit版本是测试运行器使用的版本,可能与编译期间使用的版本不同。
So, while you might be compiling your tests against version 2.6.4 , if your test runner uses version 2.6.3 , then you need to redirect to 2.6.3 instead: 因此,虽然您可能正在针对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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.