.net “创建单元测试”选项在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9547055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:17:46  来源:igfitidea点击:

Where is the "Create Unit Tests" selection?

.netunit-testingvisual-studio-2012visual-studio-2013mstest

提问by Houman

I have installed the new Visual Studio 2012 Ultimate.

我已经安装了新的 Visual Studio 2012 Ultimate。

I have created a Test Project with my solution and it contains some default unit tests. However right clicking on new Methods doesn't show the "Create Unit Tests" context menu any longer.

我用我的解决方案创建了一个测试项目,它包含一些默认的单元测试。但是,右键单击新方法不再显示“创建单元测试”上下文菜单。

See Visual Studio 2010 Ultimate:enter image description here

请参阅 Visual Studio 2010 Ultimate:在此处输入图片说明

And Visual Studio 2012 Ultimate:enter image description here

和 Visual Studio 2012 Ultimate:在此处输入图片说明

Any idea why that could be?

知道为什么会这样吗?

UPDATE:

更新:

Solution for Visual Studio 2012 and 2013:

Visual Studio 2012 和 2013 的解决方案:

Microsoft has acknowledged it hereand offers an official work around patchto fix it. Download, install and Enjoy!

微软已经在这里承认它并提供了一个官方补丁来修复它。下载、安装和享受!

采纳答案by Daniel A. White

This feature was cut from VS.

此功能是从 VS 中删除的。

http://blogs.msdn.com/b/visualstudioalm/archive/2012/03/08/what-s-new-in-visual-studio-11-beta-unit-testing.aspx

http://blogs.msdn.com/b/visualstudioalm/archive/2012/03/08/what-s-new-in-visual-studio-11-beta-unit-testing.aspx

Generate Unit Test Wizard – In VS2010 you could right click on a method in your code and we would generate a unit test into your test project. This wizard was very tightly coupled to MS-Test and depended on features like Private Accessors to do its work, so it was cut. We are exploring alternatives here, but don't have any good solutions yet.

生成单元测试向导 - 在 VS2010 中,您可以右键单击代码中的方法,我们将在您的测试项目中生成单元测试。这个向导与 MS-Test 非常紧密地结合在一起,并且依赖于像 Private Accessors 这样的特性来完成它的工作,所以它被剪掉了。我们正在这里探索替代方案,但还没有任何好的解决方案。

EDITThere is a work around: http://blogs.msdn.com/b/malaysia/archive/2013/02/20/right-click-create-unit-tests.aspx

编辑有一个解决方法:http: //blogs.msdn.com/b/malaysia/archive/2013/02/20/right-click-create-unit-tests.aspx

回答by JohnB

The "Unit Test Generator" extension is available

单元测试生成器”扩展可用

Unit Test Generator Extension

单元测试生成器扩展

To add directly from Visual Studio 2012/2013 IDE:

要直接从 Visual Studio 2012/2013 IDE 添加:

  1. Menu: Tools -> Extensions and Updates
  2. Click: Online -> Visual Studio Gallery
  3. Search for: "Unit Test Generator"
  1. 菜单:工具 -> 扩展和更新
  2. 点击:在线 -> Visual Studio Gallery
  3. 搜索:“单元测试生成器”

MSDN Video Demonstration

MSDN 视频演示

Note: MSTest in VS 2012/2013 still sucks

注意:VS 2012/2013 中的 MSTest 仍然很糟糕

回答by intrueder

The menu item is still in place, but for unknown reasons it's invisible in the code editor. But you can use visual studio commands to run unit test wizard. The command you need is

菜单项仍然存在,但由于未知原因,它在代码编辑器中不可见。但是您可以使用 Visual Studio 命令来运行单元测试向导。你需要的命令是

EditorContextMenus.CodeWindow.CreateUnitTests

Open Command Window and type the command there. Or type an alias command as follows

打开命令窗口并在那里键入命令。或者输入一个别名命令如下

alias ut EditorContextMenus.CodeWindow.CreateUnitTests

and assign the alias to run command faster.

并分配别名以更快地运行命令。

Another way to run the wizard is keyboard shortcut. In Visual Studio open

另一种运行向导的方法是键盘快捷键。在 Visual Studio 中打开

Tools -> Options -> Environment -> Keyboard

Type "createunit" in "Show commands containing" text box to filter out CreateUnitTests command, select it. Choose "Text Editor" in "Use new shortcut in" combo, and press shortcut you want in "Press shortcut keys" text field (e.g., Ctrl+T, Ctrl+C). Finally, press Assign button. Enjoy!

在“显示包含的命令”文本框中键入“createunit”以过滤掉 CreateUnitTests 命令,选择它。在“使用新快捷方式”组合中选择“文本编辑器”,然后在“按快捷键”文本字段中按所需的快捷方式(例如,Ctrl+T、Ctrl+C)。最后,按分配按钮。享受!

回答by Sebastien H.

Here is how to to enable it again :

以下是再次启用它的方法:

Method 1 - Command Window

方法 1 - 命令窗口

If you find hard enough, you will discover that the command to invoke the Create Unit Tests... is actually:

如果你足够努力,你会发现调用 Create Unit Tests... 的命令实际上是:

EditorContextMenus.CodeWindow.CreateUnitTests

EditorContextMenus.CodeWindow.CreateUnitTests

You can actually type that in the Command Window to invoke the Create Unit Test Wizard. If you find it too verbose, you can actually create an alias for it.

您实际上可以在命令窗口中键入它以调用创建单元测试向导。如果你觉得它太冗长,你实际上可以为它创建一个别名。

alias ut EditorContextMenus.CodeWindow.CreateUnitTests

别名 ut EditorContextMenus.CodeWindow.CreateUnitTests

Then just type ut to launch the wizard.

然后只需键入 ut 即可启动向导。

Method 2 - Keyboard Shortcut

方法 2 - 键盘快捷键

The next method is to go to Tools->Options->Environment->Keyboard and set a shortcut key of your preference.

下一个方法是转到工具->选项->环境->键盘并设置您喜欢的快捷键。

Method 3 - Re-enable the Create Unit Tests... Context Menu.

方法 3 - 重新启用创建单元测试...上下文菜单。

To re-enable the Create Unit Tests... menu, go to Tools->Customize... to launch the Customize dialog. At the Customize dialog, go to the Commands tab, select the Context menu radio button and choose Editor Context Menus | Code Window.

要重新启用“创建单元测试...”菜单,请转到“工具”->“自定义...”以启动“自定义”对话框。在自定义对话框中,转到命令选项卡,选择上下文菜单单选按钮,然后选择编辑器上下文菜单 | 代码窗口。

You will realized that the menu item is actually available in the context menu. The VS developers were kind enough to leave it there for us.

您将意识到菜单项实际上在上下文菜单中可用。VS 开发人员很友好地把它留给我们。

Click on the Create Unit Tests... menu item and click Move Down until it's on/under the "Run tests.." Command. Then click Close.

单击“创建单元测试...”菜单项,然后单击“下移”,直到它位于“运行测试...”命令上/下方。然后单击关闭。

Restart Visual Studio 2012 and that's it ! The context menu will be available!

重新启动 Visual Studio 2012,就是这样!上下文菜单将可用!

You may discover that using any of the above methods may not work. i.e. The Command Window will tell you that the command is not available, the keyboard shortcut will not fire or the Context menu will be available but disabled.

您可能会发现使用上述任何一种方法都可能不起作用。即命令窗口将告诉您该命令不可用,键盘快捷键不会触发或上下文菜单将可用但已禁用。

To allow all these to work, you will first need to have a Unit Test Project in your solution. After creating the Unit Test Project, you will need to manually Add a Unit Test item to the project even though the project unfolds with one on its own. After you have done this, everything will function as it is like back in Visual Studio 2010. You can safely remove those UnitTest1.cs files.

为了让所有这些工作,您首先需要在您的解决方案中有一个单元测试项目。创建单元测试项目后,您将需要手动将单元测试项目添加到项目中,即使项目单独展开。完成此操作后,一切都会像在 Visual Studio 2010 中一样运行。您可以安全地删除这些 UnitTest1.cs 文件。

Source(with images, i don't have enough Rep Points to put them) :
http://serena-yeoh.blogspot.fr/2013/02/visual-studio-2012-create-unit-test.html

来源(有图像,我没有足够的代表点来放置它们):http:
//serena-yeoh.blogspot.fr/2013/02/visual-studio-2012-create-unit-test.html

(ps : read the Caveats !!!! )

(ps:阅读注意事项!!!!)

回答by Michael Freidgeim

The answerof intruederdidn't work for many users. When I enter UT in a command window, I sometimes had the following

答案intrueder没有对许多用户的工作。当我在命令窗口中输入 UT 时,有时会出现以下情况

>UT
Command "EditorContextMenus.CodeWindow.CreateUnitTests" is not available.

But sometimes it worked?for the same solution, so behavior seemed to be intermittent.

但有时它有效?对于相同的解决方案,因此行为似乎是间歇性的。

With a help of Dawa Law's post “Create Unit Tests… Function Not Available Or Disabled" I found that during ?each??Visual Studio?session ?you need to create one test class manually before wizard will be available.

Dawa Law的帖子“创建单元测试...功能不可用或禁用”的帮助下,我发现在每次??Visual Studio?会话期间,您需要手动创建一个测试类,然后向导才可用。

If you have an existing Unit Test project in the solution,

如果您在解决方案中有一个现有的单元测试项目,

At Solution Explorer, right click the Unit Test project –>?Add –> Unit Test… This will create a new unit test class and trigger Visual Studio that an existing Unit Test class is available, thus enables Create Unit Tests wizard.? Note:?You may delete the created Unit Test class file (UnitTestX.cs) as it is used to trigger the function.

在解决方案资源管理器中,右键单击单元测试项目 –>?添加 –> 单元测试... 这将创建一个新的单元测试类并触发 Visual Studio 现有单元测试类可用,从而启用创建单元测试向导。注意:?您可以删除创建的单元测试类文件(UnitTestX.cs),因为它用于触发该功能。

If?you do not have an existing Unit Test project in the solution,?you need to create it before adding a manual class:

如果解决方案中没有现有的单元测试项目,则需要在添加手动类之前创建它:

Go to?File –> Add –> New Project… In the Add New Project window, select?Installed –> Visual C# –> Test –> Unit Test Project. Give it a meaningful name and click OK

转到?File –> Add –> New Project… 在Add New Project 窗口中,选择?Installed –> Visual C# –> Test –> Unit Test Project。给它一个有意义的名称,然后单击“确定”

The Dawa Law's post?“Create Unit Tests…” Function Not Available Or Disabledhas snapshots to illustrate each step.

达瓦法的帖子?‘创建单元测试...’功能不可用或禁用了快照来说明每一个步骤。

Note, that VS 2012 Create Unit Tests Wizard support generation tests for publicmethods only, when in previous version private methods were also supported.

请注意,VS 2012 创建单元测试向导仅支持公共方法的生成测试,而在以前的版本中也支持私有方法。

.

.

回答by Scott Dorman

The "Create Unit Tests" menu was removed in the beta. I'm not certain if there are plans to bring it back or not. If it really is a problem for you, request it on the Visual Studio UserVoice site: http://visualstudio.uservoice.com/

“创建单元测试”菜单在测试版中被删除。我不确定是否有计划将其带回来。如果这对您来说确实是个问题,请在 Visual Studio UserVoice 站点上请求它:http: //visualstudio.uservoice.com/

回答by Dawa Law

You may regain the right click "Create Unit Tests..." by importing the following vssettings from here.

您可以通过从此处导入以下 vssettings 来重新获得右键单击“创建单元测试...” 。

回答by Peter Ming

http://blogs.msdn.com/b/malaysia/archive/2013/02/20/right-click-create-unit-tests.aspx

http://blogs.msdn.com/b/malaysia/archive/2013/02/20/right-click-create-unit-tests.aspx

Method 3 - Re-enable the Create Unit Tests... Context Menu.

方法 3 - 重新启用创建单元测试...上下文菜单。

This is by far the best method ever. To re-enable the Create Unit Tests... menu, go to Tools->Customize... to launch the Customize dialog. At the Customize dialog, go to the Commands tab, select the Context menu radio button and choose Editor Context Menus | Code Window.

这是迄今为止最好的方法。要重新启用“创建单元测试...”菜单,请转到“工具”->“自定义...”以启动“自定义”对话框。在自定义对话框中,转到命令选项卡,选择上下文菜单单选按钮,然后选择编辑器上下文菜单 | 代码窗口。

回答by varini

"You may discover that using any of the above methods may not work. i.e. The Command Window will tell you that the command is not available, the keyboard shortcut will not fire or the Context menu will be available but disabled.

“您可能会发现使用上述任何一种方法都可能不起作用。即命令窗口会告诉您该命令不可用、键盘快捷键不会触发或上下文菜单可用但已禁用。

To allow all these to work, you will first need to have a Unit Test Project in your solution. After creating the Unit Test Project, you will need to manually Add a Unit Test item to the project even though the project unfolds with one on its own. After you have done this, everything will function as it is like back in Visual Studio 2010. You can safely remove those UnitTest1.cs files."

为了让所有这些工作,您首先需要在您的解决方案中有一个单元测试项目。创建单元测试项目后,您将需要手动将单元测试项目添加到项目中,即使项目单独展开。完成此操作后,一切都会像在 Visual Studio 2010 中一样运行。您可以安全地删除那些 UnitTest1.cs 文件。”

(Michael Freidgeim)

(迈克尔·弗雷德海姆)