“Could not load file or assembly” when building us

2019-03-25 06:40发布

I'm running into an issue with unit tests on our Team City (8.0.4) build server - the code builds & runs all tests locally via Resharper and nCrunch.

But when running on the server I get the following error, even though the Unity assembly exists in the same directory as the unit test assembly, and is referenced in the unit test assembly.

SetUp method failed. SetUp : System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.Practices.Unity, Version=2.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. at XXXX.Unity.UnityContainerAdapter..ctor() at XXXX.GraphExtensionsTests..ctor() in c:\TeamCityV7\Agent-1\work\f02f7e27c0bedfa2\XXXX\Graph.Tests\Extensions\GraphExtensionsTests.cs:line 44

I've confirmed the copy of Microsoft.Practices.Unity is the correct version.

I've also confirmed the assemblies are built using the full version of the framework - not using client profile.

Any ideas why Team City might be failing?

2条回答
在下西门庆
2楼-- · 2019-03-25 07:13

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-03-25 07:17

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.

查看更多
登录 后发表回答