是否可以在不依賴「複製本地」的情況下運行單元測試,在運行時加載程序集?在運行時加載相關程序集時可以運行單元測試嗎?
更多細節:
我的.NET溶液看起來像這樣:
MainProject(EXE)[取決於 'Class1Project' 和使用 'AssemblyLoaderProject' 在運行時加載它。
'Class1Project' 設置爲 '拷貝本地=假' 和 'AssemblyLoaderProject' 設置爲 '拷貝本地=真'
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);
}
}
這是錯誤消息試圖用VS2012測試運行調試它的MSTest當我得到:
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()
在這種情況下,在TestInitialize方法中調用'Loader.LoadAssemblies'。加載器使用完整路徑加載class1project,所以我不需要使用複製本地或啓用部署。你可以解決我的測試項目中包含從Class1派生的類(駐留在Class1Project中)的情況嗎?在這種情況下,當我嘗試在測試運行器中加載程序集時會發生jit和探測階段,並且失敗。 – avivr
如果我在我的主要.exe程序集中做同樣的事情,創建一個派生自Class1的類,應用程序將運行得很好。它是否嘗試僅在第一次訪問派生類(在Lodaer運行後發生)時纔對JIT派生類進行JIT操作,並且在測試環境中,當文件加載時,測試運行器會嘗試JIT所有程序集的類型?也許是爲了解析哪一個包含測試? – avivr
@avivr我相信你完全正確。測試框架正在加載測試項目中的所有類型,尋找測試。這發生在'TestInitialize'之前,因此'LoadAssemblies'在探測發生之前沒有運行並且拋出相同的異常。 –