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

时间:2021-10-09 15:38:59

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

I need some advice as to how I easily can separate test runs for unit tests and integration test in 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.

我需要一些建议,以便我可以轻松地在Visual Studio中为单元测试和集成测试分开测试运行。通常,或者总是,我构建了如上图所示的解决方案:用于单元测试和集成测试的单独项目。单元测试非常频繁地运行,而当上下文正确对齐时,自然会运行集成测试。

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). So for example

我的目标是以某种方式配置在我使用键盘快捷键时运行哪些测试(或测试文件夹)。测试最好由图形测试运行器(ReSharpers)运行。所以举个例子

  • 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.Tests中运行测试,

  • Alt+3 runs them both (i.e. 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). Being able to do this, but via a keyboard command and with a graphical test runner would be awesome.

TestDriven.net可以选择只在选定的文件夹或项目中运行测试,方法是右键单击它并选择Run Test(s)。能够做到这一点,但通过键盘命令和图形测试运行器将是非常棒的。

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

Currently I use VS2008, ReSharper 4 and nUnit. But advice for a setup in the general is of course also appreciated.

目前我使用的是VS2008,ReSharper 4和nUnit。但是,对于一般设置的建议当然也值得赞赏。

4 个解决方案

#1


2  

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. While recording I selected my [Tests] folder and ran ReSharpers UnitTest.ContextRun. This resulted in the following macro,

我实际上通过使用绑定到宏的键盘命令为我自己找到了一种解决方案。从菜单工具>宏>记录TemporaryMacro中记录宏。录制时我选择了[Tests]文件夹并运行了ReSharpers UnitTest.ContextRun。这导致了以下宏,

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. This could then easily be checked in to version control and distributed to everyone who works with the project.

然而,更令人敬畏的是一个更通用的解决方案,我可以准确配置要运行的项目/文件夹/类以及何时运行。例如,通过xml文件的方式。然后可以轻松地将其签入版本控制并分发给使用该项目的每个人。

#2


0  

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. Once you have of those tools setup, you can assigned keyboard shortcuts to the commands "Tools.ExternalCommand1", "Tools.ExternalCommand2", etc.

这是一个非常繁琐的解决方案,但您可以为要运行的每组测试配置一些外部工具。我不确定你是否能够以这种方式启动ReSharper测试运行器,但是你可以运行nunit的控制台版本。完成这些工具设置后,您可以为命令“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.

这不会真的很好地扩展,并且改变很尴尬 - 但它会为您提供运行测试的键盘快捷键。它确实应该有一个更简单的方法来做到这一点。

#3


0  

You can use a VS macro to parse the XML file and then call nunit.exe with the /fixture command line argument to specify which classes to run or generate a selection save file and run nunit using that.

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

#4


0  

I have never used this but maybe it could help....

我从来没有用过这个但也许它可以帮助....

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单元测试程序集中运行所有单元测试。”

#1


2  

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. While recording I selected my [Tests] folder and ran ReSharpers UnitTest.ContextRun. This resulted in the following macro,

我实际上通过使用绑定到宏的键盘命令为我自己找到了一种解决方案。从菜单工具>宏>记录TemporaryMacro中记录宏。录制时我选择了[Tests]文件夹并运行了ReSharpers UnitTest.ContextRun。这导致了以下宏,

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. This could then easily be checked in to version control and distributed to everyone who works with the project.

然而,更令人敬畏的是一个更通用的解决方案,我可以准确配置要运行的项目/文件夹/类以及何时运行。例如,通过xml文件的方式。然后可以轻松地将其签入版本控制并分发给使用该项目的每个人。

#2


0  

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. Once you have of those tools setup, you can assigned keyboard shortcuts to the commands "Tools.ExternalCommand1", "Tools.ExternalCommand2", etc.

这是一个非常繁琐的解决方案,但您可以为要运行的每组测试配置一些外部工具。我不确定你是否能够以这种方式启动ReSharper测试运行器,但是你可以运行nunit的控制台版本。完成这些工具设置后,您可以为命令“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.

这不会真的很好地扩展,并且改变很尴尬 - 但它会为您提供运行测试的键盘快捷键。它确实应该有一个更简单的方法来做到这一点。

#3


0  

You can use a VS macro to parse the XML file and then call nunit.exe with the /fixture command line argument to specify which classes to run or generate a selection save file and run nunit using that.

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

#4


0  

I have never used this but maybe it could help....

我从来没有用过这个但也许它可以帮助....

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单元测试程序集中运行所有单元测试。”