是否可以在没有“复制本地”依赖项的情况下运行单元测试,在运行时加载程序集?
更多细节:
我的 .net 解决方案如下所示:
MainProject (exe) [取决于“Class1Project”并在运行时使用“AssemblyLoaderProject”加载它]。
'Class1Project' 设置为 'copy local = false' 和 'AssemblyLoaderProject' 设置为 'copy local = true'
Class1Project (dll)
AssemblyLoaderProject(使用 AppDomain.CurrentDomain.AssemblyResolve 和 Assembly.LoadFrom 在运行时加载和解析依赖项程序集)
UnitTestsProject(Nunit 或 MSTest)
在单元测试项目中,我正在尝试测试“Class1Project”,并且希望将其配置设置为与 MainProject 相同。
这意味着,UnitTestProject 还使用“copy local = false”引用“Class1Project”,并使用“copy local = true”引用“AssemblyLoaderProject”,并使用它在运行时加载程序集。
但由于某种原因,单元测试无法执行,运行程序抛出 FileNotFoundException 指定它无法解析“Class1Project”程序集。
尝试调试它,我发现测试运行程序甚至没有找到告诉 AssemblyLoaderProject 加载程序集的代码。
测试代码如下所示:
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
Loader.LoadAssemblies();
Class1 cls = new Class1();
Assert.IsTrue(true);
}
}
这是我尝试在 MSTest 上使用 VS2012 测试运行程序对其进行调试时收到的错误消息:
Test Name: TestMethod1
Test FullName: UnitTestProject1.UnitTest1.TestMethod1
Test Source: c:\Users\user\Documents\Visual Studio 2012\Projects\ClassLibrary1\UnitTestProject1\UnitTest1.cs : line 13
Test Outcome: Failed
Test Duration: 0:00:00.1177608
Result Message:
Test method UnitTestProject1.UnitTest1.TestMethod1 threw exception:
System.IO.FileNotFoundException: Could not load file or assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.=== Pre-bind state information ===
LOG: User = \user
LOG: DisplayName = ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
(Fully-specified)
LOG: Appbase = file:///C:/Users/user/Documents/Visual Studio 2012/Projects/ClassLibrary1/UnitTestProject1/bin/Debug
LOG: Initial PrivatePath = NULL
Calling assembly : UnitTestProject1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 11.0\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\TESTWINDOW\vstest.executionengine.x86.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Users/user/Documents/Visual Studio 2012/Projects/ClassLibrary1/UnitTestProject1/bin/Debug/ClassLibrary1.DLL.
LOG: Attempting download of new URL file:///C:/Users/user/Documents/Visual Studio 2012/Projects/ClassLibrary1/UnitTestProject1/bin/Debug/ClassLibrary1/ClassLibrary1.DLL.
LOG: Attempting download of new URL file:///C:/Users/user/Documents/Visual Studio 2012/Projects/ClassLibrary1/UnitTestProject1/bin/Debug/ClassLibrary1.EXE.
LOG: Attempting download of new URL file:///C:/Users/user/Documents/Visual Studio 2012/Projects/ClassLibrary1/UnitTestProject1/bin/Debug/ClassLibrary1/ClassLibrary1.EXE.
Result StackTrace: at UnitTestProject1.UnitTest1.TestMethod1()