如何防止“Microsoft.VisualStudio.TestPlatform.ObjectModel.TestPlatformException:找不到测试主机”异常

问题描述 投票:0回答:2

我有一个 .NET 6.0 解决方案,用于 Web 应用程序的自动化 UI 测试。该解决方案由许多项目组成,其中一个包含所有测试,另一个是测试的日志记录实现,其他项目则包含框架具有的其他杂项依赖项。

包含测试的项目使用 NUnit 作为测试框架 - 它具有 NuGet 包 NUnit (3.13.3) 和 NUnit3TestAdapter (4.2.1)。日志记录项目还包含 NUnit 包,因为它使用 NUnit.Framework.TestContext 将日志写入每个测试运行的上下文。

我正在使用 VS 2022 来开发这个解决方案。当我执行解决方案的构建时,它会告诉我构建成功(在右下角短暂闪烁),然后立即说“检测到意外错误。请检查测试输出窗格以获取详细信息。”。如果我检查我是否可以看到:

Test project MyNamespace.Logging does not reference any .NET NuGet adapter. Test discovery or execution might not work for this project.
It's recommended to reference NuGet test adapters in each test project in the solution.
========== Starting test discovery ==========
Microsoft.VisualStudio.TestPlatform.ObjectModel.TestPlatformException: Could not find testhost
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Hosting.DotnetTestHostManager.GetTestHostProcessStartInfo(IEnumerable`1 sources, IDictionary`2 environmentVariables, TestRunnerConnectionInfo connectionInfo)
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyOperationManager.SetupChannel(IEnumerable`1 sources, String runSettings)
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyDiscoveryManager.DiscoverTests(DiscoveryCriteria discoveryCriteria, ITestDiscoveryEventsHandler2 eventHandler)
NUnit Adapter 4.2.0.0: Test discovery starting
NUnit Adapter 4.2.0.0: Test discovery complete
========== Test discovery aborted: 60 Tests found in 1 sec ==========

所以它抱怨我的日志记录项目没有测试适配器。当然,我不想要或不需要这个项目的适配器,因为它合法地不包含任何测试。为了尝试取得一些进展,我安装了与我的测试项目使用的相同的 NUnit 适配器。然后,测试输出窗格中的错误减少为:

========== Starting test discovery ==========
Microsoft.VisualStudio.TestPlatform.ObjectModel.TestPlatformException: Could not find testhost
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Hosting.DotnetTestHostManager.GetTestHostProcessStartInfo(IEnumerable`1 sources, IDictionary`2 environmentVariables, TestRunnerConnectionInfo connectionInfo)
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyOperationManager.SetupChannel(IEnumerable`1 sources, String runSettings)
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyDiscoveryManager.DiscoverTests(DiscoveryCriteria discoveryCriteria, ITestDiscoveryEventsHandler2 eventHandler)
NUnit Adapter 4.2.0.0: Test discovery starting
NUnit Adapter 4.2.0.0: Test discovery complete
========== Test discovery aborted: 60 Tests found in 1.1 sec ==========

因此安装该软件包会清除它的第一部分(但当然我并不是真的想安装它)。然后我阅读了 NUnit 文档,发现了

NonTestAssembly
属性,这似乎可能就是我所需要的。我创建了一个 AssemblyInfo.cs 文件并将其添加到我的日志记录项目的根目录中,然后在其中添加了行
[assembly: NUnit.Framework.NonTestAssembly]
。重建解决方案,但错误仍然如上。有谁知道我该怎么做才能让 VS 对此感到满意?令人沮丧的是,它在我的日志记录项目上显示了锥形烧瓶图标,这并不理想,而且也使得更难看到“构建成功”消息,因为它很快就被错误覆盖了。

我现在使用 NUnit 包是错误的吗?

c# .net visual-studio nunit
2个回答
14
投票

我也遇到了同样的问题,但当我添加 Microsoft.NET.Test.Sdk 包后,它就消失了。


0
投票

如果您的测试应用了过滤器,也可能会发生此错误。删除过滤器(CTRL+D、CTRL+F)可以解决该问题。

VS Test Explorer

© www.soinside.com 2019 - 2024. All rights reserved.