如何使用来自 Rider 或 VS 2017 的 NUnit、xUnit 或 MSTest 测试 .NET Standard 2 库?

问题描述:

我有一个使用 Azure Durable Functions 的项目,它们是仅在 .NET Standard 2 上可用.因此,它定义了可以在测试项目中使用的类库.但是,我无法将 xUnit、NUnit 或 MSTest 用于单元/集成测试的库放在一起.

I have a project where I use Azure Durable Functions, and they are available only on .NET Standard 2. So, it defines which class library can be used in testing projects. But, I cannot put together a library where either xUnit, NUnit or MSTest is used in unit/integration testing.

将 NUnit 添加到以 .NET Standard 2 为类库的项目中失败并出现以下错误:

Adding NUnit to a project where .NET Standard 2 is class library fails with the following error:

信息:恢复软件包C:VSTSgithub.com etstandardXunitMsTestNunitsrcNetstandard2xUnitMsTestnUnit unit unit.csproj...调试:正在恢复 .NETStandard、Version=v2.0 的包...调试:解决 .NETStandard,Version=v2.0 的冲突...错误:循环检测到.nunit -> NUnit (>= 3.9.0).调试:检查兼容性.NETStandard 上的软件包,版本 = v2.0.调试:检查nunit 1.0.0 与 .NETStandard 的兼容性,Version=v2.0.

INFO: Restoring packages for C:VSTSgithub.com etstandardXunitMsTestNunitsrcNetstandard2xUnitMsTestnUnit unit unit.csproj... DEBUG: Restoring packages for .NETStandard,Version=v2.0... DEBUG: Resolving conflicts for .NETStandard,Version=v2.0... ERROR: Cycle detected. nunit -> NUnit (>= 3.9.0). DEBUG: Checking compatibility of packages on .NETStandard,Version=v2.0. DEBUG: Checking compatibility for nunit 1.0.0 with .NETStandard,Version=v2.0.

xUnit 的错误是相同的(只是错误消息涉及 xUnit 循环).

The error is the same for xUnit (just the error message talks about xUnit cycle).

这两个错误也可以在 Rider 和 Visual Studio 2017 Enterprise 中重现.清理 nuget 缓存后,我再次尝试.结果是一样的.

Both error can be reproduced in Rider and Visual Studio 2017 Enterprise too. I tried it again after I cleaned nuget cache. The result is the same.

在 MsTest 的情况下,可以添加 ms 测试库,但测试发现在 Rider 和 Visual Studio 中都不起作用.

In case of MsTest, possible to add ms test libraries, but test discovery does not work neither Rider and nor Visual Studio.

是否可以对 .NET Standard 2 库进行单元测试?

除了等待这些项目使用 .NET Standard 2 之外,我还能做些什么吗?

Is there anything I can do beside waiting for these projects to pick up .NET Standard 2 stuff?

我创建了一个小示例项目,可以在这里找到:https://github.com/SayusiAndo/netstandard2xunitresharper

I created a small sample project, can be found here: https://github.com/SayusiAndo/netstandard2xunitresharper

.NET Standard 没有运行时,因此它不会执行您的测试.

There is no runtime for .NET Standard, so it will not execute your tests.

您的测试程序集必须面向可执行平台,例如 .NET Framework 或 .NET Core 的某个版本.

Your test assembly must target an executable platform, such as a version of .NET Framework or .NET Core.

<TargetFramework>net470</TargetFramework>

<TargetFramework>netcoreapp2.0</TargetFramework>

有关详细信息,请参阅在不同框架上运行 .NET Standard 二进制文件.

See Running .NET Standard binaries on different frameworks for more details.