运行 在 visual studio 中进行单元测试时出错:缺少测试用例对象

Error when running unit tests in visual studio: Test-case objects missing

当 运行 使用 resharper 从 visual studio 进行单元测试时,我收到以下错误:

Test-case objects missing for the following elements: ... Rebuild the project and try again

在单元测试方法旁边的工具提示中,它说:Inconclusive: Test not run,我认为它曾经说过 Unit test skipped。 Resharper 显示灰色眼睛图标。

visual studio 的测试资源管理器也有一个奇怪的问题。它不会显示我所有的单元测试。事实上,它缺少我的 200 多个单元测试。它们只是没有出现在测试资源管理器 window 中。我确实有一些测试出现并且会 运行 就好了。

我已经尝试过的事情:重新启动我的机器、清理、重建、更改所有依赖项目以使用相同的 .net 框架 4.7。

我正在使用 VS 2017 .net Framework 4.7、Resharper 和 MSTest。全部具有最新的更新和版本。

经过一天的努力,我让 Test Explorer 开始工作了。这是目标平台。

在单元测试项目属性中,在构建下,我将平台目标设置为 x64。我将它切换到 AnyCPU,Test Explorer 立即找到了我丢失的单元测试。但是,resharper 仍然给我同样的错误。如果我找到解决方案,我会更新。同时,我现在至少可以 运行 调试我的单元测试。

我在删除所有 nuget 包后遇到了这个错误,因为我更改了我的 .net 版本并且不需要它们中的任何一个来编译。

在 re-installing MSTest.TestAdapter nuget 包 ReSharper 启动后 运行 我的单元再次按预期进行测试。

Install-Package MSTest.TestAdapter

我知道这不会回答 OP 的问题,因为 he/she 已经尝试过了,但也许它会对其他人有所帮助。

对我来说,Visual Studio 的简单重启解决了 缺少以下元素的测试用例对象:...重建项目并重试问题。

我的问题是使用了错误的构建配置。

我的解决方案有很多项目和很多不同的构建配置,我无意中选择了错误的项目配置,该配置没有正确设置以包含我尝试使用的测试项目。

对我来说,这是因为我为 FLEE 安装了 Nuget 包,这将以下行插入到我的测试项目的 app.config 文件中:

<dependentAssembly>
  <assemblyIdentity name="System.Reflection" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
</dependentAssembly>

因为我的机器上没有安装 4.1.2.0 版(我有 4.0.0.0),所以测试项目在某个地方失败了,非常令人困惑 'error message'

一旦我安装了 FLEE 软件包的早期版本 (1.05)(使用 4.0.0.0),并且在 app.config 的 bindingRedirect 中将版本号更改为 4.0.0.0,一切又恢复正常了

我在使用 F# 项目时遇到了这个问题。在我的例子中,是测试方法名称中包含括号导致了问题;

[<TestMethod>]
member x.``E is updated where generic form or visit should include (this) UPDATED VISIT AND FORM``() =
        let origExpr = "visit:form:INTQ1AB"
        let checkedExpr = "VISIT2:FormFour:INTQ1AB"
        ...

导致了一个不确定的测试,同时删除了“(this)”周围的括号,如下所示

[<TestMethod>]
member x.``E is updated where generic form or visit should include this UPDATED VISIT AND FORM``() =
        let origExpr = "visit:form:INTQ1AB"
        let checkedExpr = "VISIT2:FormFour:INTQ1AB"
        ...

允许测试成功完成。

我已将 MSTest.TestAdapter 从 1.1.18 升级到 1.3.2。终于帮我解决了。

当我让 Resharper 通过 NuGet 搜索 Nunit 丢失的引用时,我 运行 陷入了这个问题。它没有下载我需要的包的 .NET 核心兼容版本,而是获取了 .NET 框架版本。我卸载了 NUnit 包并使用 NuGet 包管理器重新安装来解决这个问题。

将解决方案克隆到新机器上后,我遇到了这个错误。问题是我没有安装指定的 dotnet 框架——我只安装了 2.1 和 3.0,它需要 2.2。公平地说,测试输出中的错误确实报告了这一点:

Testhost process exited with error: It was not possible to find any compatible framework version
The specified framework 'Microsoft.NETCore.App', version '2.2.0' was not found.
  - The following frameworks were found:
      2.1.13 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
      3.0.0 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
You can resolve the problem by installing the specified framework and/or SDK.
The .NET Core frameworks can be found at:
  - https://aka.ms/dotnet-download
. Please check the diagnostic logs for more information.

请注意 Visual Studio 安装缺少的框架后需要重新启动。

当我更新一些测试项目以使用项目文件的新格式时,我遇到了这个问题,其中一些项目仍然需要包含 app.config 文件,当我得到 运行 测试时这些消息

  • 缺少以下元素的测试用例对象...
  • 缺少测试用例。重建项目并重试

在 app.config 中删除运行时部分中的所有程序集绑定为我修复了它 - 即在运行时部分中删除这样的条目

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>

错误应该在 Visible Studio 的输出 window 中可见。您可能需要在选项中更改为冗长。

在我的例子中,这是因为我引用了一个没有强名称的 dll。