Run specific unit test in Visual Studio

C#Visual Studio-2010

C# Problem Overview


I have dozens of unit tests, and I'd like to fix the code I am working on now, but every time I run the tests it takes over 30 seconds to run every unit test (I think reflection is the cause of some of these being especially slow). My current test and many other tests take less than one second to run, but it is annoying to disable and then enable specific unit tests.

Is there a way to say, "For now, run a single, specific test?"

I'm using Visual Studio 2010 with the built-in unit test system. I create tests by right-clicking any code and selecting "create unit test" which creates a project.

C# Solutions


Solution 1 - C#

Resharper and Test Driven .Net both have this feature in their visual studio test-runners. There may well be other VS plugins too.

According to this answer (By Jon Skeet no less) it is possible in Visual studio natively too. To debug a single test: >Click on a test method name, then press Ctrl+R, Ctrl+T. (Or go to Test / Debug / Tests in Current Context.)

EDIT: (based on a comment from Justin R below) to run a test (as opposed to debugging it) the command is simply:

>Ctrl+R, T

Solution 2 - C#

@Andrew M's answer is awesome, but I wanted to know where the keyboard shotcuts come from. All you have to do is:

  1. Right click your test function name.
  2. Select Run Test or Debug Test.

enter image description here

Solution 3 - C#

First, click somewhere inside the TestMethod you want to run, or highlight multiple methods. You can also click on the class or namespace if you want to run all TestMethods within that class/namespace.

Using The Test Tools Toolbar

  1. Make sure the Test Tools Toolbar is visible (View → Toolbars → Test Tools).

Visual Studio Test Tools Toolbar

Using The Test Menu

Run: Test → Run → Tests in Current Context

Debug: Test → Debug → Tests in Current Context

Using Shortcuts

Run: Ctrl+R,T

Debug:Ctrl+R,Ctrl+T

Solution 4 - C#

Windows: Run unit tests with Test Explorer

>Use Test Explorer to run unit tests from Visual Studio or third-party unit test projects. You can also use Test Explorer to group tests into categories, filter the test list, and create, save, and run playlists of tests. You can debug tests and analyze test performance and code coverage. more…

  1. Select one or more tests in Test Explorer
  2. right-click → Run Selected Tests or Debug Selected Tests

Run unit tests with Test Explorer on Windows


Mac: Run unit tests from Unit Test Pad or Text Editor

Option 1: Unit Test Pad

View → Pads → Unit Tests

> Test Write high-quality code with testing tools. Visual Studio for Mac’s integrated test runner helps you run and debug unit tests and automated UI tests. more…

Select one or more tests, right-click, Run Test or Debug Test.

Run unit tests from Unit Test Pad on Mac

Option 2: Text Editor Unit Test Integration

Visual Studio → Preferences → Text Editor → Source Analysis
☑ Enable text editor unit test integration

You can directly run a single test from directly from a test source file,

Solution 5 - C#

Default Keyboard Shortcut is Ctrl+R then Ctrl+T for Debug

for Run Ctrl+R then T

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser34537View Question on Stackoverflow
Solution 1 - C#Andrew MView Answer on Stackoverflow
Solution 2 - C#JessView Answer on Stackoverflow
Solution 3 - C#Drew ChapinView Answer on Stackoverflow
Solution 4 - C#SwiftArchitectView Answer on Stackoverflow
Solution 5 - C#Matas VaitkeviciusView Answer on Stackoverflow