给定目标之间的实现差异,如何正确地对具有多个目标框架的.NET项目进行单元测试? [英] How to properly unit test a .NET project with multiple target frameworks, given implementation differences among targets?

查看:199
本文介绍了给定目标之间的实现差异,如何正确地对具有多个目标框架的.NET项目进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑针对以下框架的.NET类库:




  • .NET Framework 2.0

  • .NET Framework 4.6

  • .NET Standard 1.0

  • .NET Standard 1.3

  • .NET可移植Profile336



让我们不必立即担心为什么这是目标框架的确切列表。



假设所有目标框架中的外部API都是相同的,但是,每个目标框架都有一些该框架所独有的代码,我该如何正确编写覆盖来自以下代码的单元测试所有目标框架?



我正在运行Visual Studio 2017社区。

我已经准备好了到目前为止在GitHub上的最小样本,到目前为止,我发现的样本可能是我现在能做的最好的样本: https://github.com/airbreather/MultiTargetDemo ,但这不是感觉不到做对的正确方法。一些缺陷:




  • 每个目标框架都需要一个单独的.csproj文件,并且需要大量复制粘贴。

  • 使用一个记录不充分的*属性,称为 ReferringTargetFrameworkworkProjectReferences


    • *这有点轻描淡写...目前,对其进行谷歌搜索,实际上除了Microsoft的SDK内容外什么都没有显示。


  • 似乎不能与.NET Standard垫片正常配合使用(.NET Standard 1.3在运行时抛出试图查找 System.IO.FileSystem 。 (编辑:知道了,这是 corefx问题#16322 ,其中有些一个尴尬的解决方法)。



上述链接中的库仅具有一个公共类,并且仅具有一个公共实例方法。根据合同,该方法必须仅返回32位整数值3,但它会以每个对象上的3种根本不同的方式计算出3。显然,实际用例围绕实现更多有趣的方法而展开,但这足以进行对话。



此外,请注意,该库中的测试类实际上使用了一些并非在所有目标平台上都可用的功能: System.Threading.Tasks.Task< TResult> 在.NET Framework 2.0上不可用...代理可以代替测试本身可能用于其自身目的的任何东西,在测试的目标框架中可能不一定可用。



实际用例是 NetTopologySuite ,它已经针对多个全框架平台和一些PCL;有一个年代久远的问题,要求使其与.NET Core一起使用。我一直在不停地努力使这种情况发生大约一个月,而我终于跌入了这个问题。



我发现另一个问题:使用.NET Core和xUnit同时针对多个框架的单元测试代码,确实与此相似。出于某些原因,这有所不同:




  • 该问题是使用VS2015时代的预览SDK工具(项目。 json )。这个问题是指带有漂亮的 .csproj 文件和所有文件的VS2017时代的非预览工具。

  • 可以解决这个问题通过简单地将测试项目本身与一个测试目标框架进行多目标化,以被测对象为目标。似乎无法以类似方式解决此问题,尤其是使用非常规Profile336目标时。


    • 此外,鉴于完整的.NET Framework 4.6可以引用针对列出的五个目标框架中的任何一个的程序集,因此我应该没有必要将基本测试类的实现限制为在为此选择的任何目标框架的交集中都可用。

    • 我确实尝试了类似列出合成内容的黑客攻击在 TargetFrameworks 中,例如 test1; test2; test3; test4; test5 ,然后强制 TargetFrameworkIdentifier + TargetFrameworkVersion 到.NETFramework + 4.6,但是我没有太大的成功(尝试获取 xunit <时失败了/ code>包)。不过,我也没有尝试过



解决方案

首先,测试项目也可以通过使用< TargetFrameworks> property就像您在库中使用的一样。尽管VS测试运行程序当前仅显示/运行一个框架(第一个框架是特定的),但 dotnet测试的任何调用都将执行所有框架(xunit也在开发自定义控制台跑步者- dotnet xunit -为获得更好的控制台UX。



还要记住,测试项目需要指定具有运行时的目标框架。因此,您的测试项目可以以 netcoreapp * net * 为目标,但绝不能以 netstandard * 为目标。 code>,因为它没有关联的运行时来运行测试。我不确定可移植的目标,因为曾经测试项目需要引用以获取测试运行器支持的 Microsoft.NET.Tests.Sdk 没有NETPortable的条件。



因此,您需要选择 net netcoreapp 您需要在/ support上进行测试-例如 net20; net45; net46; netcoreapp1.0 将涵盖您的所有.net框架和.net核心版本。



通过在< ProjectReference>上设置 Properties = TargetFramework = netstandard1.3 ,可以强制依赖特定的目标框架。 元素,但这对于恢复和多目标定位来说可能很难正常工作。


Consider a .NET class library that targets the following frameworks:

  • .NET Framework 2.0
  • .NET Framework 4.6
  • .NET Standard 1.0
  • .NET Standard 1.3
  • .NET Portable Profile336

Let's not immediately worry about why this is the exact list of target frameworks.

Supposing that the external API across all target frameworks is identical, but each individual target framework has some code that's unique to that framework, how do I properly write the unit tests that cover the code from all target frameworks?

I'm running Visual Studio 2017 Community.

I've cooked up a ready-to-go minimal sample on GitHub here with what I've found so far to be probably the best I can do right now: https://github.com/airbreather/MultiTargetDemo, but this doesn't feel like the right way to do it. Some flaws:

  • Requires a separate .csproj file for each target framework with lots of copy-paste.
  • Uses a not-well-documented* property called ReferringTargetFrameworkForProjectReferences.
    • *that's a bit of an understatement... Googling it at the moment, literally nothing shows up apart from Microsoft's SDK stuff.
  • Doesn't seem to work properly with the .NET Standard shims (.NET Standard 1.3 here throws at runtime trying to look for System.IO.FileSystem). (edit: figured it out, this was corefx issue #16322, which has a bit of an awkward workaround).

The library in the above-linked has precisely one public class with precisely one public instance method on it. The method is contractually obligated to just return the 32-bit integer value 3, but it computes that 3 a materially different way on each target. Obviously the actual use case revolves around implementing much more "interesting" methods, but this is sufficient for conversation.

Furthermore, note that the test class in that library actually uses some stuff on its own that's not available on all target platforms: System.Threading.Tasks.Task<TResult> is not available on .NET Framework 2.0... this serves as a proxy for just anything that the test itself might be using for its own purposes, which might not necessarily be available in the target framework that's being tested.

The actual use case is NetTopologySuite, which already targets multiple full-framework platforms and some PCLs; there's a year-old issue asking to make it work with .NET Core. I've been working on-and-off to try to make that happen for just about a month now, and I'm finally stumbling into this issue.

I found that other question: Unit testing code targeting several frameworks at once with .NET Core and xUnit, which is indeed similar to this one. This is different for a few reasons:

  • That question was using the VS2015-era preview SDK tooling (project.json). This question refers to the VS2017-era non-preview tooling with the pretty .csproj files and all.
  • That question could be resolved by simply multi-targeting the test project itself with one "test" target framework per framework targeted by the "subject-under-test". It does not appear that this question can be resolved in a similar way, especially with the unusual Profile336 target.
    • Besides, given that the full .NET Framework 4.6 can reference assemblies targeted at any of the listed five target frameworks, there should be no need for me to have to limit the implementations of the fundamental test classes to just what's available in the intersection of whatever grab-bag of target frameworks that get selected for this.
    • I did try a hack like listing synthetic stuff in TargetFrameworks like test1;test2;test3;test4;test5 and then forcing TargetFrameworkIdentifier + TargetFrameworkVersion to .NETFramework + 4.6, but I didn't have much success (it failed when trying to grab the xunit package). I also didn't try too hard on this, however.

解决方案

First, test projects can multi-target as well by making use of the <TargetFrameworks>property just like you used for the library. While the VS test runner currently only shows/runs one framework (the first one to be specific), any invocation of dotnet test will execute all frameworks (xunit is also developing custom console runner - dotnet xunit - for a better console UX).

Also keep in mind that test projects need to specify a target framework that has a runtime. So your test projects can either target netcoreapp* or net* but never netstandard* since it does not have a runtime associated to run the tests. I'm not sure about the portable target since the Microsoft.NET.Tests.Sdk that ever test project needs to reference to get test runner support doesn't have conditions for NETPortable.

So you'd need to pick all the versions of net and netcoreapp that you need to test on / support - e.g. net20;net45;net46;netcoreapp1.0 would cover all your .net framework and .net core versions.

There is a way to force a dependency to a specific target framework by setting a Properties="TargetFramework=netstandard1.3" on the <ProjectReference> element, but this may be hard to get working correctly for restore and multi-targeting.

这篇关于给定目标之间的实现差异,如何正确地对具有多个目标框架的.NET项目进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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