【问题标题】:How to resolve stack overflow problem during dotnet test?如何解决 dotnet 测试期间的堆栈溢出问题?
【发布时间】:2020-01-10 13:18:37
【问题描述】:

首先我的设置:

  • .Net Core 3.1(从 2.2 迁移而来)
  • xUnit 2.4.1
  • xUnit Runner 2.4.1
  • Visual Studio 2019 16.4.2
  • Test.SDK 16.4.0

我刚刚从 .Net Core 2.2 迁移到 3.1。我刚刚修复了错误并将我的序列化程序更改为 System.Text。我的应用运行良好,但是...

注意:我所有的测试都是使用WebApplicationFactory的集成测试。

我一直使用 Visual Studio Test Runner 来运行所有测试,但是当我尝试 Run All 时出现错误:

请注意奇怪的netcoreapp2.2,而我专门在 MyTests 和 MyApp 项目中使用 .Net Core 3.1。

[10.01.2020 2:05:03.939 PM] System.InvalidOperationException: The following TestContainer was not found 'D:\Projects\MyApp\MyTests\bin\Debug\netcoreapp2.2\GrabGoApiTests.dll'
   at Microsoft.VisualStudio.TestWindow.Client.TestContainer.TestContainerProvider.<GetTestContainerAsync>d__46.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.TestWindow.Controller.TestContainerConfigurationQueryByTestsBase.<QueryTestContainerConfigurationsAsync>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.TestWindow.Controller.TestContainerConfigurationQuery.<GetTestContainerConfigurationsAsync>d__16.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.TestWindow.Controller.TestRunConfiguration.<UpdateAsync>d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.TestWindow.Controller.RunOperation.<RunTestsAsync>d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.TestWindow.Controller.Operation.<<Execute>b__37_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.TestWindow.Extensibility.ILoggerExtensions.<CallWithCatchAsync>d__10`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception exceptionObject)

然后我在想“好吧......也许 VS 再次表现得很奇怪,我会尝试从控制台运行测试”

PS D:\Projekty\GrabGoAPI> dotnet test
Test run for D:\Projects\MyApp\MyTests\bin\Debug\netcoreapp3.1\GrabGoApiTests.dll(.NETCoreApp,Version=v3.1)
Microsoft (R) Test Execution Command Line Tool Version 16.3.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...

A total of 1 test files matched the specified pattern.
The active test run was aborted. Reason: Test host process crashed : Stack overflow.


Test Run Aborted.
Total tests: Unknown
     Passed: 101
 Total time: 26,3009 Seconds

现在我在这里。

我找到了that。但我无法在 Visual Studio 中调试测试以找出导致问题的测试。

还有dotnet test --blame 每次都指责随机测试。

如何找到导致 StackOverflow 异常的原因?

【问题讨论】:

  • 测试通常不会按照特定的顺序执行,通常会在调用堆栈达到其限制时产生stackoverflow异常。这两件事结合起来可以解释这个问题。恕我直言,这里有几个可能的原因:a)递归和b)大量测试。很难用提供的信息来判断。
  • 测试使用 .Net Core 2.2。只有 267 个测试。所以不是很多。也许问题出在 System.Text JsonSerializer 中的某个地方……我不知道……

标签: c# asp.net-core xunit dotnet-test


【解决方案1】:

我得到了这个错误,它是由一些重构后调用自身的方法引起的,所以谢谢你测试。

【讨论】:

    【解决方案2】:

    我是因为 System.Text.Json 没有像 JSON.Net 那样处理 Reference Loop

    更改为 JSON.Net 有帮助。

    【讨论】:

      【解决方案3】:

      我通过安装Microsoft.NET.Test.Sdk 解决了这个问题。

      1. 打开工具标签
      2. 选择 NuGet 包管理器 子菜单
      3. 单击为解决方案管理 NuGet 包菜单项
      4. 安装 Microsoft.NET.Test.Sdk

      【讨论】:

        猜你喜欢
        • 2019-07-02
        • 1970-01-01
        • 2015-12-25
        • 2014-12-13
        • 2012-05-09
        • 2020-01-11
        • 2022-07-20
        • 2016-09-15
        相关资源
        最近更新 更多