【问题标题】:UnitTesting assembly.GetTypes() throws an exception in the build pipeline onlyUnitTesting assembly.GetTypes() 仅在构建管道中引发异常
【发布时间】:2020-12-03 17:47:25
【问题描述】:

单元测试调用以下方法。它在本地运行时通过,但在作为 Azure DevOps 构建管道的一部分运行时由于 assembly.GetTypes() 上的 LoaderException 而失败。我不确定如何调试它,因为它不会在本地发生。通常我会在调试模式下运行它并查看 LoaderException。管道任务 VSTest@2 只记录堆栈跟踪

Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly assembly in assemblies)
{
   if (assembly != null)
   { Type[] types = assembly.GetTypes(); }
}
   Test method 
Tests.Data.IBlockConversionTests.TestIBlockConversion threw exception: 
System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
  Stack Trace:
      at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.RuntimeModule.GetTypes()
   at System.Reflection.Assembly.GetTypes()
   at Plugin.PluginLoader`1.LoadPlugins(IEnumerable`1 assemblies) in D:\a\1\s\Core\Plugin\Plugin\PluginLoader.cs:line 80
   at Tests.Data.IBlockConversionTests.TestIBlockConversion() in D:\a\1\s\Tests\Data\IBlockConversionTests.cs:line 42

【问题讨论】:

  • 首先查看LoaderException的异常消息,以及任何InnerExceptions
  • 在管道上运行单元测试时我该怎么做?
  • 此构建管道系统中是否存在日志记录工具?
  • 看看你的测试输出?捕获异常并记录您需要的所有内容?创建一个专门的控制台应用程序来重现问题并打印您需要的内容?富有想象力!
  • 将 Syste.Debug 设置为 true docs.microsoft.com/en-us/azure/devops/pipelines/build/… 以检查是否会有有趣的事情

标签: c# unit-testing azure-devops pipeline assemblies


【解决方案1】:

解决方案是通过添加 distributionBatchType: 'basedOnAssembly' 来更改 yaml 管道,如下所示。打开诊断也解决了这个问题,但不是一个理想的解决方案。

- task: VSTest@2
  inputs:
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      **\Tests\**
      !**\*TestAdapter.dll
      !**\obj\**
    searchFolder: '$(System.DefaultWorkingDirectory)'
    diagnosticsEnabled: false
    distributionBatchType: 'basedOnAssembly'

【讨论】:

  • 这也解决了我们在另一个管道上 VSTest 无法找到某些 Dll 的问题。
猜你喜欢
  • 1970-01-01
  • 2017-11-20
  • 2020-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-08
  • 1970-01-01
  • 2015-12-12
相关资源
最近更新 更多