gpt4 book ai didi

.net-core - 无法让 xUnit 测试与 .NET Core 一起运行

转载 作者:行者123 更新时间:2023-12-04 03:48:53 25 4
gpt4 key购买 nike

我正在将 NuGet 上的一个小型库移植到 .NET Core。

我为主项目和测试创建了 .NET Standard 1.6 类库,并复制了代码。我将单元测试更改为使用 xUnit 属性和断言而不是 NUnit。

除此之外,我几乎遵循了 the documentation 中的说明。 ,因此我添加了以下 NuGet 包:

  • Microsoft.NET.Test.Sdk
  • xunit
  • xunit.runner.visualstudio

  • 唉,(1) 测试资源管理器没有找到我的单元测试,以及 (2) 当我运行 dotnet test 时,我得到以下信息:

    Starting test execution, please wait... Could not find testhost.dll for source '[...].Tests.dll'. Make sure test project has a nuget reference of package "microsoft.testplatform.testhost".



    我实际上已经添加了建议的 Microsoft.TestPlatform.TestHost NuGet 包,但这并没有改变任何东西。

    那么这里的问题是什么?

    我使用的是 Visual Studio 2017。并不是说我认为它有什么不同。

    更新 : 从 Class Library (.NET Standard) 更改测试项目至 Class Library (.NET Core)解决了这个问题。我仍然不明白为什么这应该有所作为。

    最佳答案

    Changing the test project from Class Library (.NET Standard) to Class Library (.NET Core) fixed the problem. I still don't get why this is supposed to make a difference.



    单元测试是我们运行的应用程序。要构建这样的应用程序,我们必须指定运行时和应用程序模型。当我们面向 .NET Standard 时,运行时和应用程序模型是不明确的; MSBuild 不知道是针对 .NET Framework、.NET Core、Mono/Xamarin 还是其他符合 .NET Standard 的平台进行构建。面向 .NET Core 为 MSBuild 提供了所需的输入,它现在知道如何解析所有引用的程序集/项目并选择适当的框架版本。

    But that was never the case. In the past, you would always use a class library for unit tests; it was never considered an application that you could run, but rather a collection of classes and methods that you would feed into a test runner.



    过去我们没有 .NET Standard,这是一个不明确的目标。当 MSBuild 看到 .NET Standard 时,它需要更多信息。 “好的,您想使用哪个 .NET Standard 兼容的运行时来生成可运行的输出?”例如,如果我们的目标是 netstandard1.2 ,那么 MSBuild 将不知道是否针对 .NET Core 1.0、.NET Framework 4.5.1、Windows 8.1 或其他几个构建 netstandard1.2合规平台。

    enter image description here

    Starting test execution, please wait... Could not find testhost.dll for source '[...].Tests.dll'. Make sure test project has a nuget reference of package "microsoft.testplatform.testhost".



    如果我们不指定 netcoreapp ,那么 MSBuild 假定我们使用的是完整的框架。在这种情况下,它需要目标程序集,包括 testhost.dll , 在 bin .如果它们不是(如果我们针对 .NET Standard 构建它们也不会),那么我们将收到上述错误。

    关于.net-core - 无法让 xUnit 测试与 .NET Core 一起运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42938879/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com