"Could not load file or assembly" when building using team city

Check the pattern you're using to locate your test assemblies. I had a similar problem with another library and it turns out the pattern was finding the test assembly under bin\Release and obj\Release; the obj folder doesn't contain all the assemblies referenced by the project and is really just a scratch folder for the compiler.


Another possibility I recently discovered is if you are using a library that you are not explicitly referencing, TeamCity will not collect the library for use and fail when it is implicitly referencing the assembly. I discovered this when trying to figure out why NHibernate.ByteCode.Castle, which was referenced in my test project, was not being loaded and resulting in a FileNotFoundException on TeamCity. Ultimately, I made a "dummy" unit test like so:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using NHibernate.ByteCode.Castle;

namespace MyNamespace
{
    [TestClass]
    public class CastleProxyPresenceTest
    {

        [TestMethod]
        [TestCategory("Infrastructure: This test forces TeamCity to load the NHibernate.ByteCode.Castle.dll file.")]
        public void CastleProxyLoads()
        {
            var dummy = new LazyFieldInterceptor();
            Assert.IsNotNull(dummy);
        }
    }
}

...after this, the file loaded properly and my unit tests compiled.