I have been meaning to blog about this error for a few days. Only got around to do so now. Recently, after a changeset was checked in, we starting seeing the following exception in our Team Build
An exception occurred while invoking executor 'executor://mstestadapter/v1': Object reference not set to an instance of an object.
On the face of it, we couldn't find spot anything obviously wrong in the changeset. The solutions compiled correctly on local machine and all the tests executed successfully when ran from Visual Studio. However, in the team build the unit tests would fail.
Since, we are using TFS 2010 and our test include Visual Studio 2012 functionality, we are using the VSTest activity of TfsBuildExtensions to execute our unit tests. Behind the scenes, the activity executes the vstest.console.exe process passing all the test assemblies as command line parameters. This means that all our unit tests are executed in a single process in a single test run.
Looking closely, we spotted that some of our assemblies were targeted to .Net 4.0 and some of the assemblies were targeted to .Net 4.5 and we were using Entity Framework 5.0 in both these assemblies. Entity framework includes different versions of its library for assemblies targeting .Net 4.0 as compared to .Net 4.5. This is what the problem was. If a version of Entity Framework dll is already loaded to the process, the other version of the library could not be loaded and this is what caused the error. The solution is to run tests in different test runs.
I have create a simple project to demonstrate this issue. You can download from here. The error is very obvious if vstest.console.exe is executed from command line.