Run specific unit test in Visual Studio - c#

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.

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

#Andrew M's answer is awesome, but I wanted to know where the keyboard shotcuts come from. All you have to do is:
Right click your test function name.
Select Run Test or Debug Test.

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
Make sure the Test Tools Toolbar is visible (View → Toolbars → Test Tools).
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

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…
Select one or more tests in Test Explorer
right-click → Run Selected Tests or Debug Selected Tests
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.
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,

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

Related

SpecFlow Test cannot be run with Playwright.NET

I created a specflow project using c# and playwright. No issues with the code, but the tests do not run When using test explorer on visual studio.
when i run the test, the results always say:
test not run
I installed the nuget packages for playwright and specflow and cannot see any errors. Are there any other components required for the test to run?
I m using playwright.net with c#
any advise would be appreciated
sample feature file:
Feature: feature example
#mytag
Scenario: Open test
Given the test page is open
When I navigate to the links page
Then I will see all the links
thanks
1- Are the tests ignored?
If you have r# Open one of the test files and check if there is a little gray eye-like icon to the left of test method declarations, if so, tests are ignored.
if not on r#, open vs text explorer and check if your tests are there, and if they are, there is something on the window that says they are ignored.
This weirdly happens when your file path is too long (i.e deep in the folder hierarchy, or maybe not so eep, but long folder and file names.
try to move you test project up in the folder hierarchy on the drive.
2- Do you have a test runner for the test framework? i.e nunit test runner?
If you don't see your tests on the test explorer at all, try installing test framework's runner as a vs extension.
Have you tried to move the solution upper in the directory hierarchy?
ie.
c:\projects\mycompany.projects.crmprojects.solution1\project1\testproject....\test.cs
becomes
c:\projects\solution1\project1\test.cs

Visual Studio Debug Single Test

I use to be able to right-click in one of my test classes and select Debug Test which would run the single test. All of a sudden it says Debug Test(s) when I right-click and runs all my tests.
How can I debug just one test?
Thanks
Microsoft Visual Studio Community 2017
You can right click directly on your [TestMethod] and select "Run test(s)"/"Debug test(s)", or open the Test Explorer window (from the menu: Test -> Windows -> Test Explorer) and run test method from there (selecting a group or a single one).
PS: remember to put at least one breakpoint inside the test you want to debug.
Not sure why but I had to reinstall the NuGet Test packages and that seemed to fix it. Thanks Manuel for the help.

Recording EVERY test scenario run using intelliTest in Visual Studio 2015 RC

While watching the intellitest explorer window during a test run, I notice that Pex runs more tests than is displayed when the run is finished. How do I ensure that all of the tests that were run are displayed in the window when run is complete? I would like to examine the intermediate tests that failed/passed and also save them for further use.
Intellitest works on the basis of code coverage as the metric. It shows a test only if:
Test covers a code path not previously covered
Test is not duplicate
So the the results window actually shows all the tests covering code paths intellitest can reason about. If there's a statement in code intellitest cannot reason, it shows a warning and no test is generated.
There is a slightly different behavior if you do a "Run Intellitest" at a class level. In that case, tests are generated for each method in the class and the generated tests shown correspond to the method selected in drop down (e.g. currently only tests for XmlObjectContext.Dispose are shown).

How to run test methods (in Visual Studio) in Microsoft Test Manager?

I'm using Visual Studio 2012 Ultimate and Microsoft Test Manager. In my project, I have written a test class with some test methods (i.e. Assert this and Assert that).
How do I actually run them in Microsoft Test Manager? I have created a Test Plan and added Test Cases, but how do I actually link my test methods to the test case? I've already looked online and on the Microsoft website, but it wasn't helpful and they were skipping so many steps.
What do I need to configure in Visual Studio? in Microsoft Test Manager?
The test case must be associated with an automated test. MTM test cases have fields for "automation status" and "associated automation", but the later cannot be set from within MTM, it has to be set from Visual Studio.
Open Team Explorer in Visual Studio and use it to find and open the test case with a query. On the right hand side of the "associated automation" tab there is an ellipsis, click it and select the test to be run. See here for more details. Note that when the test case is opened within MTM the ellipsis is not shown. I like to think of "pushing test automation from Visual Studio into MTM" being the method and that test cannot be "pulled into MTM".
Visit this URL
Here it is very well explained how to associate a test method to a test case.

NUnit debugging a single test

I use NUnit plugin from ReSharper. I can't find any way of debugging a single test. The BUG button always launches all the tests, even when I launch the debug specifically from one test method.
I'm trying to reach a breakpoint with one specific test and I don't want to reach it with the other tests.
Do you know any way of doing this? Google didn't help me on this one...
Example of my test code
[Test]
public void IsValidDoer_DoerValid()
{
var mockRepositoryDoer = new Mock<IDoerRepository>();
mockRepositoryDoer.Setup(c => c.ActiveDoers).Returns(activeDoers.AsQueryable);
var doerValidation = new DoerValidation(mockRepositoryDoer.Object);
Assert.IsTrue(dModel.IncludedDoers.Any());
}
[Test]
public void IsValidDoer_DoerInvalidNoQuota()
{
var mockRepositoryDoer = new Mock<IDoerRepository>();
var activeDoers = listDoers.ToList();
activeDoers.First().QuotaActivity.Clear();
mockRepositoryDoer.Setup(c => c.ActiveDoers).Returns(activeDoers.AsQueryable);
var doerValidation = new DoerValidation(mockRepositoryDoer.Object);
Assert.IsFalse(dModel.IncludedDoers.Any());
}
Yes Alongside the code is a green and yellow mark just click this and click run it will run that single test. You just left click it once you will get options and depending on what you also have installed from Jetbrains you could launch code coverage from here too.
You can also choose to append it to an already existing session of other tests or create it in a session all on its own.
Clarification:
Someone downvoted this so I went back and took a look and tested it both for MSTEST and NUnit. it is true it is not desirable to execute the 15 tests if you only wish to debug one. The test was conducted in visual studio 2015 with Resharper 10 and visual studio 2013 with Resharper 8. If you click on the mark in the individual test file it will indeed only run the code once.
If you run multiple tests and get a test session in Resharper's runner with three tests it will, on right click, show "debug tests", however if you only select one it only runs one test and so only hitting the code only once.
We also where having the same issue when using ReSharper 10 as the test runner. It would run all the tests, even if I configured only a single test in the session. Also it would run all tests when I used the right click on the test ball for this test.
After installing the NUnit3 Test Adapter under Tools->Extensions and Updates->Online I could debug the tests from the regular Test Explorer of Visual Studio 2015 by right clicking and selecting Debug selected tests. This does only run this one test :)
I had same issue. When trying to debug single test by clicking on the circle and selecting 'Debug (bug icon)' it would Debug for all unit tests.
I resolved it by upgrading Resharper. I upgraded from Resharper Ultimate 10.0.1 to 10.0.2.
Try to right-click the code of the specific test. You should see "Debug Unit Test" or something like this in the context menu.

Categories