Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).
I'm using AutoData attribute on one of the dummy test cases

[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?

like image 740
dragan.stepanovic Avatar asked Sep 25 '22 06:09

dragan.stepanovic


1 Answers

That exception is caused by NUnit not loading the AutoFixture add-in at runtime so the test parameters don't get any arguments.

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:

<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.

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:

<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>
like image 122
Enrico Campidoglio Avatar answered Nov 11 '22 15:11

Enrico Campidoglio