简体   繁体   English

无法向 .NET Core 单元测试项目 VS2017 添加单元测试

[英]Cannot add a unit test to a .NET Core Unit Test Project VS2017

Current status: Templates are missing from .NET Core当前状态:.NET Core 中缺少模板

I have come to the conclusion that these are missing (among other non-test templates see Item templates are missing ).我得出的结论是这些都丢失了(在其他非测试模板中,请参阅项目模板丢失)。

I have now raised a seperate issue on GitHub to make these test templates available to .NET Core projects and up to the level where the .NET Framework templates are.我现在在 GitHub 上提出了一个单独的问题,使这些测试模板可用于 .NET Core 项目,并达到 .NET Framework 模板所在的级别。 You can follow the progress here: Unit test templates missing from .NET Core Unit test project您可以在此处查看进度: .NET Core 单元测试项目中缺少单元测试模板

Hopefully this will be helpful to someone missing these templates in .NET Core.希望这对在 .NET Core 中缺少这些模板的人有所帮助。

For clarity this is what I've tried为清楚起见,这是我尝试过的

  • Trying multiple installs of Visual Studio 2017 (on different computers)尝试多次安装 Visual Studio 2017(在不同的计算机上)
  • Asking colleagues to reproduce same behaviour要求同事重现相同的行为
  • Ticking variety of different options on the Visual Studio installer在 Visual Studio 安装程序上勾选各种不同的选项
  • Plenty of Googling大量的谷歌搜索

The Problem:问题:

If I Add a New "MSTest Test Project(.NET CORE)" to my solution, then right click the newly generated projected goto "Add" > "New Item" the "Add New Item" window is displayed, however I cannot find the test templates for adding a unit test (2nd Screenshot shows these templates, this is a .NET Framework project) At the moment I am having to Copy and Paste a current unit test file and then rename it.如果我将新的“MSTest 测试项目(.NET CORE)”添加到我的解决方案中,然后右键单击新生成的投影转到“添加”>“新项目”,则会显示“添加新项目”窗口,但是我找不到用于添加单元测试的测试模板(第二个屏幕截图显示了这些模板,这是一个 .NET Framework 项目)目前我必须复制并粘贴当前的单元测试文件,然后重命名它。

This is only an issue with .NET CORE test projects.这只是 .NET CORE 测试项目的一个问题。

Have tried the following:尝试了以下方法:

  • Gone through the Visual Studio installer making sure there is nothing missing通过 Visual Studio 安装程序确保没有任何遗漏
  • up to date with VS2017 updates最新的 VS2017 更新
  • updated the MSTest Adapter NUGET packages更新了 MSTest 适配器 NUGET 包

Note: This person ( Unit testing features missing for .NET Core in VS 2019 and Visual Studio Installer? ) is also getting the same issue as I am but with VS2019!注意:此人( VS 2019 和 Visual Studio 安装程序中的 .NET Core 缺少单元测试功能? )也遇到了与我相同的问题,但使用 VS2019!

Just to confirm: Where is Create Unit Test in VS 2017?只是为了确认: VS 2017 中的创建单元测试在哪里? is NOT the same issue.不是同一个问题。 This is the "Create Unit Test" context menu.这是“创建单元测试”上下文菜单。

.NET Core 单元测试项目的“添加新项”屏幕的屏幕截图

.NET 框架单元测试项目的“添加新项目”屏幕的屏幕截图

因为 UnitTest 类与常规类仅通过TestClass装饰器不同,所以我认为 UnitTest 类没有模板。

Current status: Templates are missing from .NET Core当前状态:.NET Core 中缺少模板

I have come to the conclusion that these are missing (among other non-test templates see Item templates are missing ).我得出的结论是这些都丢失了(在其他非测试模板中,请参阅项目模板丢失)。

I have now raised a seperate issue on GitHub to make these test templates available to .NET Core projects and up to the level where the .NET Framework templates are.我现在在 GitHub 上提出了一个单独的问题,使这些测试模板可用于 .NET Core 项目,并达到 .NET Framework 模板所在的级别。 You can follow the progress here: Unit test templates missing from .NET Core Unit test project您可以在此处查看进度: .NET Core 单元测试项目中缺少单元测试模板

Hopefully this will be helpful to someone missing these templates in .NET Core.希望这对在 .NET Core 中缺少这些模板的人有所帮助。

For clarity this is what I've tried为清楚起见,这是我尝试过的

  • Trying multiple installs of Visual Studio 2017 (on different computers)尝试多次安装 Visual Studio 2017(在不同的计算机上)
  • Asking colleagues to reproduce same behaviour要求同事重现相同的行为
  • Ticking variety of different options on the Visual Studio installer在 Visual Studio 安装程序上勾选各种不同的选项
  • Plenty of Googling大量的谷歌搜索

baruchiro (5 May 2019) explained: baruchiro(2019 年 5 月 5 日)解释说:

Because the UnitTest class different from a regular class just by the TestClass decorator, I think there is no template for UnitTest class.因为 UnitTest 类与常规类仅通过 TestClass 装饰器不同,所以我认为 UnitTest 类没有模板。

It appears baruchiro is correct: As of 25 July 2019, according to a post on Visual Studio Developer Community , those templates probably are not coming back. baruchiro 似乎是正确的:截至 2019 年 7 月 25 日,根据Visual Studio Developer Community上的一篇帖子,这些模板可能不会再回来了。

There has been clean up of a lot of menu items and these options have been removed from .NET Framework projects as well in VS2017 as part of overall cleanup of less used/low value options.已经清理了许多菜单项,并且这些选项已从 .NET Framework 项目中删除,并且在 VS2017 中作为整体清理较少使用/低值选项的一部分。 At this time we do not intend to add these option back.目前我们不打算重新添加这些选项。

I fear the OP may have already found the best solution/workaround at this time (copy test class and rename).我担心 OP 目前可能已经找到了最佳解决方案/解决方法(复制测试类并重命名)。

Remember to:记得:

  • rename both the .cs file as well as the class reference inside the file.重命名 .cs 文件以及文件内的类引用。
  • include the using statement to the item you're wanting to test.在要测试的项目中包含 using 语句。

These test templates are missing (at least in VS2019), so you can just make a normal class and use below as a template:缺少这些测试模板(至少在 VS2019 中),因此您可以创建一个普通类并使用以下作为模板:

 using Microsoft.VisualStudio.TestTools.UnitTesting; using <class_to_test>; namespace <class_to_test>Tests { [TestClass] public class <class_to_test>Test { [TestMethod] public void TestMethod1() { } } }

you should replace <class_to_test> for the name of your class.您应该将<class_to_test>替换为您的班级名称。 It is good practice to name your test same as your class + Test .将测试命名为与 class + Test相同的名称是一种很好的做法。 Make sure that your class is public, otherwise the test will not be started.确保您的课程是公开的,否则不会开始测试。

You might have to add the project you want to test dependencies, if you did not already do that.您可能必须添加要测试依赖项的项目,如果您还没有这样做的话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM