简体   繁体   English

Visual Studio 2019 单元测试因 InvalidProgramException 而失败

[英]Visual Studio 2019 unit test fails with InvalidProgramException

Has anyone experienced with the below unit test execution error both in Visual Studio IDE 2019 (16.9.3) and command line through vstest.console.exe (16.9.1)?有没有人在 Visual Studio IDE 2019 (16.9.3) 和命令行中通过 vstest.console.exe (16.9.1) 遇到以下单元测试执行错误?

This happens even after installing the latest .NET 5.0.5 (5.0.202 SDK) following this only related post .即使在此唯一相关帖子之后安装最新的 .NET 5.0.5 (5.0.202 SDK) 后,也会发生这种情况。

I even tried to debug into the Newtonsoft 13.0.1 but only to find the constructor just quits and throws the exception here .我什至尝试调试到 Newtonsoft 13.0.1 但只是发现构造函数刚刚退出并在此处抛出异常。 It does not even hit the JContainer ctor here .它甚至没有在这里击中 JContainer ctor。

The same logic runs fine during runtime but does not through/within a test [Fact] function.相同的逻辑在运行时运行良好,但无法通过/在测试中运行 [事实] function。 I extracted it out to make sure it is isolated and does not go through any mock/fakes.我将其提取出来以确保它是隔离的,并且不会通过任何模拟/伪造品 go。 Seems like something with the .NET test engine somewhere? .NET 测试引擎似乎在某处?

The failing unit test project is using the following:失败的单元测试项目使用以下内容:

  • xunit (2.4.1) xunit (2.4.1)
  • xunit.runner.visualstudio (2.4.3) xunit.runner.visualstudio (2.4.3)
  • NSubstitute (4.2.2) NSubstitute (4.2.2)
  • mstest.testframework (2.2.3) mstest.testframework (2.2.3)
  • Mircrosoft.NET.Test.Sdk (16.9.4)微软.NET.Test.Sdk (16.9.4)
  • Microsoft.QualityTools.Testing.Fakes (16.7.4-beta.20330.2) Microsoft.QualityTools.Testing.Fakes (16.7.4-beta.20330.2)

It is a .NET FW 4.6.2 targeting project with csproj file setup with .NET SDK style instead of the old .NET FW structure.这是一个 .NET FW 4.6.2 目标项目,其 csproj 文件设置为 .NET SDK 样式,而不是旧的 Z303CB0EF9EDB925D908 结构。

Stack trace from the unit test command line execution:来自单元测试命令行执行的堆栈跟踪:

Error Message:
   System.InvalidProgramException : Common Language Runtime detected an invalid program.
  
Stack Trace:
   at Newtonsoft.Json.Linq.JContainer..ctor(JContainer other)
   at Newtonsoft.Json.Linq.JObject..ctor(JObject other)
   at Newtonsoft.Json.Linq.JObject.CloneToken()
   at Newtonsoft.Json.Linq.JContainer.EnsureParentToken(JToken item, Boolean skipParentCheck)
   at Newtonsoft.Json.Linq.JContainer.InsertItem(Int32 index, JToken item, Boolean skipParentCheck)
   at Newtonsoft.Json.Linq.JContainer.TryAddInternal(Int32 index, Object content, Boolean skipParentCheck)
   at Newtonsoft.Json.Linq.JContainer.Add(Object content)
   at Newtonsoft.Json.Linq.JArray.Add(JToken item)
   at at MyUnitTests.<MyTest_HasNeededData_ReturnsData>d__45.MoveNext() in XXXXXXXXXXXXXXXXXXXXXXX:line 643
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()

Please help.!!请帮忙。!! Thanks in advance.提前致谢。

After testing with a brand new test project, it turns out the issue was because of the inclusion/usage of a MS Fakes-generated assembly of the Newtonsoft.Json (seems to be regardless of version eg Newtonsoft.Json.13.0.0.Fakes.dll)在使用全新的测试项目进行测试后,事实证明问题是因为包含/使用了 MS Fakes 生成的 Newtonsoft.Json 程序集(似乎与版本无关,例如 Newtonsoft.Json.13.0.0.Fakes .dll)

I was relying on this fake to intercept some of the Newtonsoft extension methods for a few test cases.我是靠这个假的来拦截一些测试用例的一些 Newtonsoft 扩展方法。 At this time, I am able to remove the usage/dependency on this fake assembly so the issue can go away for now.在这个时候,我可以删除这个假程序集的使用/依赖,所以这个问题现在可以解决。

Not sure why it would happen or the exact root cause.不确定为什么会发生或确切的根本原因。 I have all tests executed in a single threaded across multiple classes (through xUnit collection grouping) and MS Fakes supposes to be thread isolated so don't know what the deal is.我已经在跨多个类的单个线程中执行了所有测试(通过 xUnit 集合分组),并且 MS Fakes 应该是线程隔离的,所以不知道交易是什么。 Maybe I have to report this to both Microsoft and Newtonsoft to see if any of them would take on the task for a fix.也许我必须向 Microsoft 和 Newtonsoft 报告这件事,看看他们中的任何一个是否会承担修复任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带伪造的单元测试中的Visual Studio 2015 InvalidProgramException - Visual Studio 2015 InvalidProgramException in Unit Test With Fakes 使用 Visual Studio 2019 对 BizTalk 2020 工件进行单元测试导致 Intellisense 问题 - Unit test BizTalk 2020 artefacts using Visual Studio 2019 leads to Intellisense issue Visual Studio 2019 中的单元测试项目模板仅适用于 .NET Framework 是否重要? - Does it matter that Unit Test project template in Visual Studio 2019 is for .NET Framework only? 在 Visual Studio (2019) 中调试单元测试项目时,在同一解决方案中运行另一个项目 - Run another project in the same solution while debugging unit test project in visual studio (2019) 使用Visual Studio 2019构建现有的Sln失败 - Building existing sln fails with Visual Studio 2019 单元测试,NUnit或Visual studio? - Unit test, NUnit or Visual studio? 如何在 c# Visual Studio 2019 中的单元测试方法中使用控制台或终端来显示 output 并接收用户的输入? - How to use the console or terminal inside of a unit test method in c# visual studio 2019 to display output and receive input from user? Visual Studio单元测试-“ XUnit格式?” - Visual Studio Unit Test - “XUnit format?” 在Visual Studio 2012中运行单元测试的问题 - Issue with running Unit Test in Visual Studio 2012 Visual Studio单元测试锁定dll - Visual Studio Unit Test locking dll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM