在 VS2019 (版本 16.3.9) 開了新的單元測試專案,在 Test Explorer 可以看到測試方法,但測試無法執行。按下測試鈕後,測試無聲無息結束,執行測試數為 0,也沒傳回錯誤。同一個 Solution 有另一個 .NET Core 寫的 MSTest 單元測試 則一切正常測試。

Output 視窗的訊息以 The program '[xxxx] testhost.x86.exe' has exited with code -1 (0xffffffff).,結束,看不出所然。 總體來說,測試似乎因某種原因出了差錯所以沒執行,但 Visual Studio 大方回報已完成 0 個測試就回家休息,也沒說原因。

試著將 Logging Level 調高到 Trace (Include Platform logs):

再跑一次測試,依然是無聲無息結束,但這回在 Output 看到錯誤訊息 The program '[xxxx] testhost.x86.exe' has exited with code -1 (0xffffffff).

確認出錯後,在變詳細的 Log 找到以下錯誤:

TpTrace Warning: 0 : 248252, 3, 2020/05/12, 08:42:49.615, 21283133786021, testhost.x86.exe, TestPluginDiscoverer: Failed to load extensions from file 
'C:\Users\jeffrey\.nuget\packages\mstest.testadapter\2.0.0\build\_common\Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.dll'.
  Skipping test extension scan for this file.  
  Error: System.IO.FileLoadException: 無法載入檔案或組件 'file:///C:\Users\jeffrey\.nuget\packages\mstest.testadapter\2.0.0\build\_common\Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.dll' 或其相依性的其中之一。 
  不支援作業。 (發生例外狀況於 HRESULT: 0x80131515)
檔案名稱: 'file:///C:\Users\jeffrey\.nuget\packages\mstest.testadapter\2.0.0\build\_common\Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.dll' ---> 
System.NotSupportedException: 嘗試從在舊版 .NET Framework 中會導致以沙箱處理組件的網路位置載入組件。這個版本的 .NET Framework 預設為不啟用 CAS 原則,所以這種載入可能會有危險。如果這項載入原意並非要以沙箱處理組件,請啟用 loadFromRemoteSources 參數。如需詳細資訊,請參閱 http://go.microsoft.com/fwlink/?LinkId=155569。
   於 System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   於 System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   於 System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   於 System.Reflection.Assembly.Load(AssemblyName assemblyRef)
   於 Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.TestPluginDiscoverer.GetTestExtensionsFromFiles[TPluginInfo,TExtension](String[] files, Dictionary`2 pluginInfos)

由 nuget、2.0.02、舊版... 等關鍵字,讓我想到要檢查 NuGet 套件版本:

如上圖所示,MSTest.TestAdapter 及 MSTest.TestFramework 最新版為 2.1.1,而我新建單元測試專案的版本只到 1.3.2,在升級 2.1.1 版後問題消失。

我另外找了一台 Visual Studio 2019 16.5.4 新建 Unit Test 專案,觀察其預設 MSTest.TestAdapter 及 MSTest.TestFramework 版本為 2.1.0,由此推論這問題應該只會發生未更新的 Visual Studio 2019 上,更新 VS2019 到新版可避免困擾。

Case of unit test project doesn't run in olde version Visual Stduio 2019 (16.3.9) , upgrading MSTest.TestAdapter and MSTest.TestFramework version from 1.3.2 to 2.2.1 to solve the problem.


Comments

Be the first to post a comment

Post a comment