简体   繁体   中英

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

Current status: Templates are missing from .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. You can follow the progress here: Unit test templates missing from .NET Core Unit test project

Hopefully this will be helpful to someone missing these templates in .NET Core.

For clarity this is what I've tried

  • Trying multiple installs of Visual Studio 2017 (on different computers)
  • Asking colleagues to reproduce same behaviour
  • Ticking variety of different options on the Visual Studio installer
  • 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.

This is only an issue with .NET CORE test projects.

Have tried the following:

  • Gone through the Visual Studio installer making sure there is nothing missing
  • up to date with VS2017 updates
  • updated the MSTest Adapter NUGET packages

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!

Just to confirm: Where is Create Unit Test in 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

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. You can follow the progress here: Unit test templates missing from .NET Core Unit test project

Hopefully this will be helpful to someone missing these templates in .NET Core.

For clarity this is what I've tried

  • Trying multiple installs of Visual Studio 2017 (on different computers)
  • Asking colleagues to reproduce same behaviour
  • Ticking variety of different options on the Visual Studio installer
  • Plenty of Googling

baruchiro (5 May 2019) explained:

Because the UnitTest class different from a regular class just by the TestClass decorator, I think there is no template for UnitTest class.

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.

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. 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).

Remember to:

  • rename both the .cs file as well as the class reference inside the file.
  • include the using statement to the item you're wanting to test.

These test templates are missing (at least in VS2019), so you can just make a normal class and use below as a template:

 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. It is good practice to name your test same as your 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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