Search code examples
f#nunitassembly-binding-redirect

NUnit tests fail because of DLL loading issue within F# library


I've ran into this issue before when using NUnit with F#:

Could not load file or assembly 'nunit.framework, Version=2.6.3.13283

I have downgraded my version of FSharp.Core to a lower version number. I'm not sure what I need to do to get my tests to run without any exceptions.

Code

module Foo

open NUnit.Framework
open FsUnit

[<Test>]
let ``some test``() =
    1 |> should equal 1

Packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="FSharp.Core" version="4.0.0.1" targetFramework="net46" />
  <package id="FsUnit" version="1.4.1.0" targetFramework="net46" />
  <package id="NUnit" version="2.6.4" targetFramework="net46" />
  <package id="NUnitTestAdapter" version="2.0.0" targetFramework="net46" />
  <package id="xunit" version="2.1.0" targetFramework="net46" />
  <package id="xunit.abstractions" version="2.0.0" targetFramework="net46" />
  <package id="xunit.assert" version="2.1.0" targetFramework="net46" />
  <package id="xunit.core" version="2.1.0" targetFramework="net46" />
  <package id="xunit.extensibility.core" version="2.1.0" targetFramework="net46" />
  <package id="xunit.extensibility.execution" version="2.1.0" targetFramework="net46" />
  <package id="xunit.runner.visualstudio" version="2.1.0" targetFramework="net46" />
</packages>

Solution

  • This is most likely because FsUnit references an older version of nunit.framework (2.6.3), but your applcation is compiled against a newer version of NUnit (2.6.4).

    The way to solve this is to add app.config to your test project, which will map all versions of NUnit to the one that you are referencing in your project. Something like this:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
      </startup>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="nunit.framework"  
                 publicKeyToken="96d09a1eb7f44a77" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-9999.9999.9999.9999" 
                 newVersion="2.6.4.14350" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    </configuration>
    

    I was solving the exact same problem in one project recently, so the above might just work for you - but if no, check what is your version of nunit.framework.dll using a tool like ILSpy. I have 2.6.4.14350, but you might have a different one.