无法让 xUnit 测试与 .NET Core 一起运行 [英] Can't get xUnit tests to run with .NET Core

查看:21
本文介绍了无法让 xUnit 测试与 .NET Core 一起运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

除此之外,我几乎完全按照

<块引用>

开始执行测试,请稍候...无法找到源[...].Tests.dll"的 testhost.dll.确保测试项目具有包microsoft.testplatform.testhost"的 nuget 引用.

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

I'm porting a small library that I have on NuGet to .NET Core.

I've created .NET Standard 1.6 class libraries for the main project and the tests, and copied over the code. I changed the unit tests to use xUnit attributes and asserts rather than NUnit.

Other than that, I pretty much followed the instructions in the documentation, and thus I added the following NuGet packages:

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

Alas, (1) Test Explorer does not find my unit tests, and (2) when I run dotnet test, I get the following:

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".

I have actually added the suggested Microsoft.TestPlatform.TestHost NuGet package, but that hasn't changed anything.

So what is the problem here?

I'm using Visual Studio 2017. Not that I think it makes a difference.

Update: 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.

解决方案

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.

Unit tests are applications that we run. To build such an application, we must specify a runtime and application model. When we target .NET Standard, the runtime and application model are ambiguous; MSBuild does not know whether to build against .NET Framework, .NET Core, Mono/Xamarin, or another .NET Standard compliant platform. Targeting .NET Core provides the required input to MSBuild, which now knows how to resolve all the referenced assemblies/projects and choose the appropriate framework version.

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.

In the past we did not have .NET Standard, which is an ambiguous target. When MSBuild sees .NET Standard it needs more information. "Okay, which .NET Standard compliant runtime do you want to use to produce the runnable output?" If for instance we had targeted netstandard1.2, then MSBuild would not know whether to build against .NET Core 1.0, .NET Framework 4.5.1, Windows 8.1, or the several other netstandard1.2 compliant platforms.

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".

If we do not specify netcoreapp, then MSBuild assumes that we are using the full framework. In that case, it expects the target assemblies, including testhost.dll, to be in the bin. If they are not (and they will not be if we built against .NET Standard), then we will receive the above error.

这篇关于无法让 xUnit 测试与 .NET Core 一起运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆