用于单元测试 .net 标准库的框架/视觉工作室项目类型? [英] Framework/visual studio project type to use for unit testing .net standard libray?

查看:21
本文介绍了用于单元测试 .net 标准库的框架/视觉工作室项目类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于 .net 标准库 的实现是由带下划线的实现(.Net framework.Net Core(windows/Linux 等)).并且这些 API 的行为可能几乎没有什么不同.TimeZoneInfo.Id 根据操作系统(我上次检查)提供不同的结果.

Given that the implementation of the .net standard library is provided by the underlined implementation (.Net framework, .Net Core (windows/Linux etc) ). And it is possible that the APIs have little different behaviour. TimeZoneInfo.Id provides different results based on the OS (last time I checked).

那么,如何进行单元测试?在 Visual Studio 中,我们可以创建 .Net Core xUnit 项目或 .Net Framework xUnit 项目.如何在两个不同的环境中运行相同的 xUnit 测试?

So, how to unit test? In visual studio we can either create a .Net Core xUnit project or .Net Framework xUnit project. How to run the same xUnit tests in two different environment?

推荐答案

最简单的方法是多目标测试项目.即从模板创建.NET Core xUnit 项目并更改

The simplest way is to multi-target the test project. That is, create a .NET Core xUnit project from the template and change

<TargetFramework>netcoreapp1.1</TargetFramework>

<TargetFrameworks>netcoreapp1.1;net461</TargetFrameworks>

请注意此处对复数/附加 s 的更改.复数形式允许您指定多个目标框架进行测试,因此您可以轻松添加多个版本的 .NET Core 和 .NET Standard.

Note the change to plural / additional s here. The plural form allows you to specify multiple target frameworks to test on, so you could easily add multiple versions of .NET Core and .NET Standard.

虽然集成 VS 的测试运行器目前只会运行第一个框架(请参阅 此跟踪GitHub 上的问题),控制台上的 dotnet test 将在所有指定的配置上运行您的测试.

While the VS-integrated test runner will only run the first framework at the moment (see this tracking issue on GitHub), dotnet test on the console will run your tests on all specified configurations.

这篇关于用于单元测试 .net 标准库的框架/视觉工作室项目类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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