Ran into an annoying issue while working on some unit tests today. It involves unit test projects within Team System for Developers. Skip to the bottom for the TL;DR version.
I was doing some general project maintenance, as is needed when multiple developers are touching the same codebase. Somewhere someone had changed the build configuration to Release and I didn't notice it after I did my usual Get Latest Version. The result of this was that my unit tests were failing due to invalid references.
The cause of the failure wasn't obvious from the beginning, so I went about rechecking and "fixing" the references until I realized what the real cause of the issue was. So, I changed the build configuration back to Debug and continued working.
I added some new code and tests, and went to the Test View window to run all unit tests in our project. While most of the tests completed successfully, a few of the test projects failed all their tests with the following errors:
System.IO.FileLoadException: Could not load file or assembly 'Blahbety.Blah, Version=0.1.0.0, Culture=neutral, PublicKeyToken=1234567890abcdef' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
This was prefixed by a message about the test failing, whether because the exception was thrown or because an unexpected exception was thrown, depending on what test was run.
Unfortunately, Google was mute on the subject. Most FileLoadExceptions have nothing to do with unit tests, so I couldn't hone in on relevant articles. The errors did all have to do with the same support DLL, but when I examined the DLL in the test folder against what the error message reported was missing, the assembly's strong names matched.
It got even weirder. I couldn't get the tests to work in Test View, but I could get them to pass if I ran them from within the test project. Selecting the project within the solution explorer and then running the tests via the Test Tools Toolbar worked.
After a bit more futzing around, I checked out the errors and warnings report from the test run. Sure enough, down at the bottom, I got a hint about what was going on:
Conflict during test run deployment: deployment item 'C:\vs2k5\Blabbety\Blabbety.Support\obj\Debug\Blabbety.Support.dll' cannot be deployed to 'Blabbety.Support.dll' because otherwise it would overwrite deployment item 'C:\vs2k5\Blabbety\Blabbety.Tests.Job\bin\Release\Blabbety.Support.dll'.
Firstly, I have no idea why the test was trying to copy a DLL from the obj directory and not the bin directory. And, to go along with this weirdness, a release binary (which was of course not the expected build) was being used to run tests against. I deduced that one of the projects was referencing an incorrect assembly, possibly through someone browsing to a binary when adding a reference instead of using the Projects tab in the Add Reference dialog.
Examining all references to the Support project I found that one of the test projects referenced the release build of the DLL. I re-created that reference using the Projects tab, but the tests still failed. The last step I had to do was refresh the tests in Test View, which finally solved the problem.
TL;DR version: If your tests throw a FileNotFound exception, your references are screwed up. Do the following:
-
Determine the DLL that cannot be loaded
-
Find all projects in your solution (including test projects) that reference this DLL
-
Check the properties of the reference to make sure it points to the correct build type (most likely Debug), and if not
-
Delete the reference and re-create it using the Projects tab of the Add Reference dialog. DO NOT BROWSE TO THE DLL!