无法使用 .NET Core 对 运行 进行 xUnit 测试
Can't get xUnit tests to run with .NET Core
我正在将我在 NuGet 上的一个小型库移植到 .NET Core。
我已经为主项目和测试创建了 .NET Standard 1.6 class 库,并复制了代码。我更改了单元测试以使用 xUnit 属性和断言而不是 NUnit。
除此之外,我几乎按照 the documentation 中的说明进行操作,因此我添加了以下 NuGet 包:
- Microsoft.NET.Test.Sdk
- xunit
- xunit.runner.visualstudio
唉,(1) Test Explorer 找不到我的单元测试,(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 提供所需的输入,MSBuild 现在知道如何解析所有引用的 assemblies/projects 并选择合适的框架版本。
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 时,它需要更多信息。 "Okay, which .NET Standard compliant runtime do you want to use to produce the runnable output?" 例如,如果我们的目标是 netstandard1.2
,那么 MSBuild 将不知道是针对 .NET Core 1.0、.NET Framework 4.5.1、Windows 8.1 还是其他几个 netstandard1.2
兼容平台。
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 构建它们将不会),那么我们将收到上述错误。
我的问题是一样的,输出消息 > 测试 控制台:
Microsoft.VisualStudio.TestPlatform.ObjectModel.TestPlatformException:
Unable to find [test-dir]\bin\Debug\netcoreapp3.1\testhost.dll.
Please publish your test project and retry.
我有 xUnit
基于包的测试:
- xunit (2.4.1)
- xunit.runner.visualstudio (2.4.2)
对我有什么帮助:
- 删除 包:
Microsoft.NET.Test.Sdk
- 安装 包:
Microsoft.TestPlatform.TestHost
现在可用,而且 更快 !
VS.NET中所有基于 xUnit 的测试项目包:
在 NuGet 包管理器中:
我正在将我在 NuGet 上的一个小型库移植到 .NET Core。
我已经为主项目和测试创建了 .NET Standard 1.6 class 库,并复制了代码。我更改了单元测试以使用 xUnit 属性和断言而不是 NUnit。
除此之外,我几乎按照 the documentation 中的说明进行操作,因此我添加了以下 NuGet 包:
- Microsoft.NET.Test.Sdk
- xunit
- xunit.runner.visualstudio
唉,(1) Test Explorer 找不到我的单元测试,(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 提供所需的输入,MSBuild 现在知道如何解析所有引用的 assemblies/projects 并选择合适的框架版本。
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 时,它需要更多信息。 "Okay, which .NET Standard compliant runtime do you want to use to produce the runnable output?" 例如,如果我们的目标是 netstandard1.2
,那么 MSBuild 将不知道是针对 .NET Core 1.0、.NET Framework 4.5.1、Windows 8.1 还是其他几个 netstandard1.2
兼容平台。
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 构建它们将不会),那么我们将收到上述错误。
我的问题是一样的,输出消息 > 测试 控制台:
Microsoft.VisualStudio.TestPlatform.ObjectModel.TestPlatformException:
Unable to find [test-dir]\bin\Debug\netcoreapp3.1\testhost.dll.
Please publish your test project and retry.
我有 xUnit
基于包的测试:
- xunit (2.4.1)
- xunit.runner.visualstudio (2.4.2)
对我有什么帮助:
- 删除 包:
Microsoft.NET.Test.Sdk
- 安装 包:
Microsoft.TestPlatform.TestHost
现在可用,而且 更快 !
VS.NET中所有基于 xUnit 的测试项目包:
在 NuGet 包管理器中: