简体   繁体   English

如何在Visual Studio中分别在不同的文件夹/项目中运行(单元)测试?

[英]How do I run (unit) tests in different folders/projects separately in Visual Studio?

Visual Studio文件夹结构

I need some advice as to how I easily can separate test runs for unit tests and integration test in Visual Studio. 我需要一些建议,以了解如何在Visual Studio中轻松地将单元测试和集成测试的测试运行分开。 Often, or always, I structure the solution as presented in the above picture: separate projects for unit tests and integration tests. 我经常或总是按照上图所示构建解决方案:用于单元测试和集成测试的单独项目。 The unit tests is run very frequently while the integration tests naturally is run when the context is correctly aligned. 当上下文正确对齐时,单元测试非常频繁地运行,而集成测试自然而然地运行。

My goal is to somehow be able configure which tests (or test folders) to run when I use a keyboard shortcut. 我的目标是以某种方式能够配置使用键盘快捷键时要运行的测试(或测试文件夹)。 The tests should preferably be run by a graphical test runner (ReSharpers). 这些测试最好应由图形测试运行程序(ReSharpers)运行。 So for example 所以举个例子

  • Alt+1 runs the tests in project BLL.Test, Alt + 1在项目BLL.Test中运行测试,
  • Alt+2 runs the tests in project DAL.Tests, Alt + 2在项目DAL中运行测试。
  • Alt+3 runs them both (ie all the tests in the [Tests] folder, and Alt + 3都运行它们(即[Tests]文件夹中的所有测试,以及
  • Alt+4 runs the tests in folder [Tests.Integration]. Alt + 4在文件夹[Tests.Integration]中运行测试。

TestDriven.net have an option of running just the test in the selected folder or project by right-clicking it and select Run Test(s). TestDriven.net可以通过右键单击并选择“运行测试”来仅在所选文件夹或项目中运行测试。 Being able to do this, but via a keyboard command and with a graphical test runner would be awesome. 能够做到这一点,但是通过键盘命令和图形测试运行程序将是很棒的。

TestDriven.net测试运行输出

Currently I use VS2008, ReSharper 4 and nUnit. 目前,我使用VS2008,ReSharper 4和nUnit。 But advice for a setup in the general is of course also appreciated. 但是,对于一般设置的建议当然也值得赞赏。

I actually found kind of a solution for this on my own by using keyboard command bound to a macro. 我实际上是通过使用绑定到宏的键盘命令自己找到了一种解决方案。 The macro was recorded from the menu Tools>Macros>Record TemporaryMacro. 宏是从菜单工具>宏>记录TemporaryMacro记录的。 While recording I selected my [Tests] folder and ran ReSharpers UnitTest.ContextRun. 在录制时,我选择了[Tests]文件夹并运行ReSharpers UnitTest.ContextRun。 This resulted in the following macro, 这产生了以下宏,

Sub TemporaryMacro()
    DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate
    DTE.ActiveWindow.Object.GetItem("TestUnitTest\Tests").Select(vsUISelectionType.vsUISelectionTypeSelect)
    DTE.ExecuteCommand("ReSharper.UnitTest_ContextRun")
End Sub

which was then bound to it's own keyboard command in Tools>Options>Environment>Keyboard. 然后在“工具”>“选项”>“环境”>“键盘”中将其绑定到它自己的键盘命令。

However, what would be even more awesome is a more general solution where I can configure exactly which projects/folders/classes to run and when. 但是,更棒的是更通用的解决方案,其中我可以准确配置要运行哪些项目/文件夹/类以及何时运行。 For example by the means of an xml file. 例如通过xml文件。 This could then easily be checked in to version control and distributed to everyone who works with the project. 然后可以轻松地将其检入版本控制,并分发给与项目合作的每个人。

This is a bit of fiddly solution, but you could configure some external tools for each of group of tests you want to run. 这是一个简单的解决方案,但是您可以为要运行的每个测试组配置一些外部工具 I'm not sure if you'll be able to launch the ReSharper test runner this way, but you can run the console version of nunit. 我不确定是否可以通过这种方式启动ReSharper测试运行程序,但是可以运行nunit的控制台版本。 Once you have of those tools setup, you can assigned keyboard shortcuts to the commands "Tools.ExternalCommand1", "Tools.ExternalCommand2", etc. 设置完这些工具后,即可为命令“ Tools.ExternalCommand1”,“ Tools.ExternalCommand2”等分配键盘快捷键。

This wont really scale very well, and it's awkward to change - but it will give you keyboard shortcuts for running your tests. 这确实无法很好地扩展,并且很难更改-但是它将为您提供用于运行测试的键盘快捷键。 It does feel like there should be a much simpler way of doing this. 确实感觉应该有一种更简单的方法来执行此操作。

您可以使用VS宏来解析XML文件,然后使用/ fixture命令行参数调用nunit.exe来指定要运行的类或生成选择保存文件,然后使用该类来运行nunit。

I have never used this but maybe it could help.... 我从来没有使用过,但也许可以帮忙...。

http://www.codeplex.com/VS2008UnitTestGUI http://www.codeplex.com/VS2008UnitTestGUI

"Project Description This project is about running all unit test inside multiple .NET Unit tests assembly coded with Visual Studio 2008." “项目说明此项目是关于在用Visual Studio 2008编码的多个.NET单元测试程序集中运行所有单元测试。”

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

相关问题 如何使单元测试在Visual Studio中连续运行 - How do I get unit tests to run continuously in Visual Studio 如何在Visual Studio 2015中的“创建单元测试”上下文菜单中添加其他测试项目? - How can I add additional Test projects to the “Create Unit Tests” context menu in Visual Studio 2015? 如何将Visual Studio解决方案分解为不同的物理文件夹和项目 - How to break up a Visual Studio Solution into Different Physical Folders and Projects 如何在 Visual Studio 中控制 C# 项目的 output 文件夹? - How do I control the output folders for C# projects in Visual Studio? Visual Studio 2017:使用启动项目运行测试 - Visual Studio 2017: Run tests with Startup Projects 如何在 Visual Studio 2017/2019 中为特定目标框架运行单元测试? - How do you run unit tests for a specific target framework in Visual Studio 2017/2019? 在Visual Studio中运行应用程序时如何运行单元测试 - How to run unit tests when running the application in visual studio 在Visual Studio中运行JavaScript单元测试 - Run JavaScript unit tests inside of Visual Studio 如何在Visual Studio 2013上为单元测试设置Emgu CV? - How do I set up Emgu CV on Visual Studio 2013 for Unit Tests? 如何在命令行中运行通常在Visual Studio测试资源管理器中运行的.cs测试? - How do I run .cs tests that I normally run in the Visual Studio Test Explorer, in the command line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM