Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does redirecting assembly binding work for unit testing with a test runner?

Ok, so here's the full description of the problem I'm having:

I am trying to use NUnit ExtensionMethods but whenever I run a test containing one of the extension methods using TestDriven.Net or if I just flat out try to load the assembly using a test-runner GUI (Icarus or NUnit) I get a FileNotFoundException.

Pounding head against a wall and digging in further I think I know what's wrong. Cue reflector and yep, I can see that NUnit.Framework>ExtensionMethods.dll has a reference to

nunit.framework, Version=2.4.6.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77

and my current version of nunit that I'm including is

nunit.framework, Version=2.4.8.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77

Now I have never used assembly re-direction before but it seems like it would be a simple matter of adding an App.Config with the following lines:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <runtime>
        <assemblyBinding  xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity
                    name="nunit.framework.dll"
                    publicKeyToken="96d09a1eb7f44a77" />
                <bindingRedirect oldVersion="2.4.6.0" newVersion="2.4.8.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

and it is my understanding that calls to the 2.4.6 version (which does not exist on this machine) should automatically redirect to the 2.4.8 version.

This does not work however, and I suspect (but have not yet confirmed) that this is because test runners do not automatically read app.config files.

So my question's are as follows:

  1. Am I right in my diagnosis of the problem?

  2. Is assembly redirection the appropriate solution and am I doing it right?

  3. How do I get this to work with the test runner?

like image 500
George Mauer Avatar asked Jan 21 '09 15:01

George Mauer


People also ask

What do binding redirects do?

1 or a later version, the app uses automatic binding redirection. This means that if two components reference different versions of the same strong-named assembly, the runtime automatically adds a binding redirection to the newer version of the assembly in the output app configuration (app.

How do I enable and disable automatic binding redirection?

Right-click the project in Solution Explorer and select Properties. On the Application page, uncheck the Auto-generate binding redirects option. If you don't see the option, you'll need to manually disable the feature in the project file.

How do I add-BindingRedirect?

Open the NuGet Package Manager Console, specify the appropriate Default Project and enter the command Add-BindingRedirect. As if by magic, an app. config is added to the project (if one doesn't exist already) and the appropriate information added. Sweet!


1 Answers

This should work if you put the configuration settings in the correct .config file. Which one that is depends on the environment you are using to run the tests, but both NUnit and TestDriven.NET should support using testassembly.dll.config.
As for this is the appropriate solution, I would say yes. The only other possibility would be to use a publisher policy file, but you would need the private key used to compile NUnit.

like image 70
csgero Avatar answered Oct 16 '22 19:10

csgero