简体   繁体   English

使用NUnit和AutoData的AutoFixture会抛出TargetParameterCountException

[英]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.

相关问题 Nunit组合引发TargetParameterCountException - Nunit Combinatorial throws TargetParameterCountException 具有参数的AutoFixture AutoData - AutoFixture AutoData with arguments AutoFixture 和 XUnit2 - 结合 TheoryData 和 AutoData - AutoFixture and XUnit2 - Combining TheoryData and AutoData 您可以在AutoFixture中将内联值与AutoData结合吗? - Can you combine inlined values with AutoData in AutoFixture? AutoFixture - 创建“有效”和“无效”实例和 [AutoData] - AutoFixture - Creation of “Valid” and “Invalid” instances and [AutoData] 如何将基于约定的自定义项与AutoFixture的[AutoData]属性结合在一起? - How to combine a Convention-based Customization with AutoFixture's [AutoData] attribute? 使用 AutoFixture 和 AutoData 时如何生成默认的 UTC DateTime? - How to generate default UTC DateTime when using AutoFixture and AutoData? 来自 Autofixture 声明性自动数据参数的属性的集合大小 - Collection size from attribute for Autofixture declarative autodata parameter AutoFixture将PropertyData与多个条目和AutoData混合(使用AutoMoqCustomization) - AutoFixture mixing PropertyData with multiple entries and AutoData (using AutoMoqCustomization) PropertieInfo的GetValue抛出TargetParameterCountException(System.Reflection) - GetValue of PropertieInfo throws TargetParameterCountException (System.Reflection)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM