c# - VS 2017 中的创建单元测试在哪里?

标签 c# visual-studio unit-testing .net-core visual-studio-2017

我知道这个问题之前已经在 SO 上被问过,而且这个功能似乎在某个时候从 VS 中删除了。但我正在查看 Microsoft tutorial现在,它表示 VS 2017 中应该有一个创建单元测试功能。

我正在尝试测试 ASP.NET Core MVC Controller 。我已按照创建测试项目的步骤进行操作。但是当我右键单击命名空间/类/方法时,我仍然看不到任何这样的选项。

编辑:我使用的是 Visual Studio 2017 社区版

最佳答案

以下答案仅与 .NET Core/Standard 相关。如果您缺少完整 .NET Framework 项目 see here 的“创建单元测试”选项。

当前状态:它现在适用于 .NET Core!

我现在在 Visual Studio Professional 2019 版本 16.3.0 预览版 3.0 上运行的 Core 3 解决方案中有创建单元测试上下文菜单。不确定它何时再次出现,因为我一直在定期更新每个新的预览版本。

历史

有关更多历史记录和详细信息,请参阅下面的更新。

原始答案 2017 年 6 月

这个来自 Microsoft's Jayarani Garg 的 GitHub 条目确认它仅适用于针对完整 .NET 框架的项目:

Jayarani Garg [MSFT] · Feb 27 2017 at 06:09 AM Hi,

Thank you for your feedback. "Create Unit Test" is currently not supported for .Net Core projects.

根据同一 Visual Studio 开发人员社区页面上的 this Visual Studio Team comment,这是设计使然,而不是错误:

Visual Studio Team ♦♦ · Mar 10 2017 at 01:06 PM

Thank you for your feedback! The Visual Studio team has determined that this issue is not a bug. However, we will consider this feedback and have created https://github.com/Microsoft/vstest/issues/592 to track this. Please feel free to vote for the issue.

上面提到的 Microsoft VS 测试存储库 Create Unit Test Context Menu Missing (.net core projects) 上的 github 问题有点令人困惑。它也要求为 .NET Core 项目添加此功能,但随后 Microsoft 员工讨论了对问题的修复。我相信该员工指的是旧错误,其中上下文菜单选项不适用于完整的 .NET 框架项目 as discussed in this question,该线程中也提到了这一点。据我所知,该线程并不是说他们已经为 .NET Core 项目添加了菜单选项。

我还刚刚查看了 Visual Studio Professional 2017 版本 15.2 (26430.6) 的副本,并且我确实为一个以完整 .NET 框架为目标的项目提供了“创建单元测试”选项(这个目标是 .NET Framework 4.5.1):

Create Unit Test menu item present

但我没有它用于一个以 .NETCoreApp 1.1 为目标的项目:

Create Unit Test menu item missing

2018 年 6 月更新

Microsoft 已注意到对 feature request 的支持,并计划在 15.8 版中为 .NET Core 项目引入上下文菜单选项:

Manish Jayaswal [MSFT] replying to Daniel Tibi · Apr 06 2018 at 10:30 PM

Getting this functionality added to .NET Core projects requires some significant changes in the new project system - which drives the .NET Core projects. This work is getting tracked in project system GitHub repo in this issue (https://github.com/dotnet/project-system/issues/3425) . This issue is expected to be resolved in 15.8 release timeframe so unfortunately, create unit test functionality for .NET Core projects would not be available in the upcoming 15.7 release. We fully understand that this is a key feature and would do our best to make it available as early as possible.

2018 年 7 月更新

Create unit test method stubs with the Create Unit Tests command 页面有 been updated now 以反射(reflect)这不适用于 .NET Core。

2018 年 8 月更新

正如下面 @gartenriese 所指出的,Microsoft 描述为跟踪此工作的 GitHub 问题 3425 已移至 15.9 milestone on 02 August 2018

2019 年 1 月更新

单元测试功能似乎依赖于 another part of the development 或被其阻止。该部分现在计划用于 VS 16.1,即 VS 2019,并且在优先级列表中也比 EF Core 3.0(这是 VS 16 的主要功能之一)according to this GitHub post by Microsoft's David Kean 更靠后。

2019 年 5 月更新

针对 .NET Core 和 .NET Standard 的项目(尚)不支持“创建单元测试”上下文菜单选项。

文档是 updated to reflect this in July 2018,现在声明:

The Create Unit Tests menu command:

  • Is available in the Community, Professional, and Enterprise Editions of Visual Studio 2015 and later.
  • Supports only C# code that targets the .NET Framework.
  • Is extensible, and supports emitting tests in MSTest, MSTest V2, NUnit, xUnit format.
  • Is not yet available in .NET Core projects.

此功能是 Visual Studio 的 slated for release in Version 15.9,但现在是 appears to have been pushed back to Version 16.1

我已经在 Visual Studio Professional 2019 RC(版本 16.0.0)的 .NET Core 2.2 项目中对此进行了测试,我可以确认它仍然无法正常工作 - 正如上述答案所预期的那样。

我还刚刚测试了 Visual Studio Professional 2019 预览版(版本 16.1.0 预览版 2.0),它在那里也不可用,所以看起来 Microsoft 还没有做到这一点。

关于c# - VS 2017 中的创建单元测试在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42876897/

相关文章:

javascript - DART - 单元测试异常

android - 如何使用 Robolectric 检测以编程方式调用 onBackPressed?

c++ - 如何使用_mm_extract_epi8函数?

visual-studio - 我可以在Visual Studio 2008中定义自定义“周围环境”模板吗?

c++ - OpenCv Unspecified error(节点不代表用户对象(未知类型?))

javascript - 开 Jest typescript 检查类型

c# - 如何使用 Linq 合并两个 XElement 列表?

c# - Uri规范化压缩FTP方案

c# - 当参数直接定义为方法参数而不是模型时,IHttpActionResult 结果无法绑定(bind)请求正文参数

C# 参数化查询 X 需要未提供的参数 Y