Condividi tramite


Unit testing signed assemblies

I recently encountered this silly issue and thought could write a small blog over it.  My team-mate wrote unit test methods in VS 2005 for his component and surprised to see the test resulted in failure when run.  Failure message is relating to loading the assembly.  All the projects under the solution are strong named.  When the unit test framework run the test over these assemblies, they actually run it over instrumented assemblies.  When unit test method calls the method lying inside the assembly, clr throws security exception sensing that the strong named assembly is being tampered (due to the process of intrumenting). 

Fix is simple; re-sign the instrumented assemblies.  Way to do it is as follows.  Go to menu - 'Test' >> 'edit testrun configuration' >> and select the test run configuration.  In the dialog window, go to >> 'Code coverage' pane and select all the assembly(s) those are signed (To note: here I need to select all the strong named assemblies that are internally referenced by the component assembly which is subject to testing).  Check the 're-sign assembly' checkbox and select the key file used to sign the assemblies.  Apply changes and run the unit test again.  Now, the unit test runs successfully.  Behind the scene, unit test framework re-signs the assemblies with the provided key file after instrumenting them.