gpt4 book ai didi

c# - 如何正确测试库的 .NET Standard 和 Core 版本?

转载 作者:行者123 更新时间:2023-11-30 17:25:37 24 4
gpt4 key购买 nike

背景:

我有一个库,它同时针对 .NET Standard 2.0 和 .NET Core(以及其他)。由于 .NET Standard 2.0 缺少 .NET Core/Framework 中存在的许多功能,我从 .NET Standard 2.0 版本的许多成员那里抛出 PlatformNotSupportedException

问题 1:

UnitTest 项目无法以 .NET Standard 为目标,因此在测试项目中,我将 .NET Core 2.0 用于 .NET Core 和 Standard 版本。

在库 .csproj 中,我需要手动更改目标:

library.csproj:
<PropertyGroup>
<!-- Compiles everything; for testing everything but .NET Standard 2.0 -->
<TargetFrameworks>net35;net40;net45;netcoreapp2.0;netstandard2.0;netstandard2.1</TargetFrameworks>
<!-- For testing .NET Standard 2.0: -->
<!--<TargetFrameworks>netstandard2.0</TargetFrameworks>-->
</PropertyGroup>

test.csproj:
<PropertyGroup>
<!-- now 'netcoreapp2.0' references either the .NET Core 2.0 or Standard 2.0 version depending on library.csproj -->
<TargetFrameworks>net35;net40;net45;netcoreapp2.0;netcoreapp3.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\library\library.csproj" />
</ItemGroup>

问题 2:

考虑到单元测试项目不能以 .NET Standard 为目标,此代码不起作用:

[Test]
public void TestSomethingThatIsNotAvailableInNetStandard20()
{
TestDelegate testCode = () => ...;

#if NETSTANDARD2_0 // This is never true
Assert.Throws<PlatformNotSupportedException>(testCode);
#else
Assert.DoesNotThrow(testCode);
#endif
}

相反,现在我使用这样的东西:

[Test]
public void TestSomethingThatIsNotAvailableInNetStandard20()
{
TestDelegate testCode = () => ...;

#if NETCOREAPP2_0 // .NET Core 2.0 OR .NET Standard 2.0
if (IsNetStandard20)
Assert.Throws<PlatformNotSupportedException>(testCode);
else
Assert.DoesNotThrow(testCode);
#else // all other targets
Assert.DoesNotThrow(testCode);
#endif
}

...

// yikes... :/
public static bool IsNetStandard20 => typeof(SomeTypeFromMyLib).Assembly.GetReferencedAssemblies()
.Any(an => an.Name == "netstandard" && an.Version == new Version(2, 0, 0, 0));

问题:

有没有一种方法可以在不一直更改 .csproj 文件的情况下测试 .NET Standard 2.0 版本(如果可能的话,去掉 IsNetStandard20 属性)?

免责声明:

我知道通常发布 .NET Standard 2.0 版本以支持 .NET Core 应用程序就足够了,但不幸的是,它缺少许多 .NET Core 2.0 具有的功能(尽管这些功能包含在 .NET Standard 2.1 中,所以不需要单独的 .NET Core 3.0 版本)。

最佳答案

A UnitTest project cannot target .NET Standard so in the test project I use .NET Core 2.0 both for the .NET Core and Standard versions.

这种说法并不完全正确。单元测试项目可以以 .NETStandard 为目标,但大多数测试平台无法找到或执行这些测试。

针对 .NETCore 的代码可以在 .NETCore 进程中显式测试。另一方面,以 .NETStandard 为目标的代码只能在使用实现指定 .NETStandard 版本的运行时的进程中隐式测试。如果您想测试后者,您需要在所有匹配的运行时上运行您的测试,您希望这些运行时与您的库的使用者一起使用。

如果您想让您的单元测试项目以 .NETStandard 为目标,那么您应该查看 Nuclear.Test .这允许您针对 .NETStandard 构建单元测试,并让它们在实现您选择的 .NETStandard 版本的 .NETCore .NETFramework 版本上执行。

您可以按照您已经采用的方式将您的单元测试项目定位到 .NETStandard 2.0 和 .NETCoreApp 2.0。您的测试将与您的第一个示例非常相似。

[TestMethod]
void TestSomethingThatIsNotAvailableInNetStandard20()
{
TestDelegate testCode = () => ...;

#if NETSTANDARD2_0 // This is compiled into net standard test assembly
Test.If.ThrowsException(testCode, out PlatformNotSupportedException ex);
// Assert.Throws<PlatformNotSupportedException>(testCode);
#else // This is compiled into every other test assembly
Test.IfNot.ThrowsException(testCode, out PlatformNotSupportedException ex);
// Assert.DoesNotThrow(testCode);
#endif
}

这种方法将解决您所描述的问题。但是,由于单元测试平台的变化,可能会引入新的问题。

关于c# - 如何正确测试库的 .NET Standard 和 Core 版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58166709/

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