Execute only one unit test while debugging in Visu

2020-02-24 11:37发布

问题:

Is there a way to select which TestMethods you want to execute in Visual Studio 2008 Unit Test project while debugging? I want to debug one particular test without having my other TestMethods execute during each debug session.

回答1:

Click on a test method name, then press Ctrl+R, Ctrl+T. (Or go to Test / Debug / Tests in Current Context.)



回答2:

CTRL-R + T = Run test in context. This can easily be triggered inside a test.
CTRL-R + A = Run all tests
CTRL-R + C = Run all tests in class
CTRL-R + N = Run all tests in namespace
CTRL-R + F = Run all checked tests (good for rerunning al failing tests)

CTRL-R + CTRL-T = Debug test in context, ie. it also attaches debugger
This also works for all other combinations.



回答3:

There are probably other ways, but I install TestDriven.NET. It allows you to right click on a test method (MSTest, NUnit, MbUnit, etc) and select Run in Debugger. I highly recommend it.



回答4:

You've got two options when running tests. Look at your Test Tools toolbar. There are four buttons: Run tests in current context, debug tests in current context, run all tests, and debug all tests in solution.

When you want to debug a single test, you can put your cursor in the body of the test and click one of the "current context" test run buttons. To run all tests in a class, put the cursor at the test class name and click one of those buttons.

Alternatively, you can create a test list (Test->Create New Test List), and use that to select the tests you want to run. You can also use theis to disable and remove tests.



回答5:

If you want to debug while running your tests under an ASP.NET solution, check out the MSDN article "How to: Debug while Running a Test in an ASP.NET Solution" at http://msdn.microsoft.com/en-us/library/ms243172.aspx.

I had to do this tonight and partially followed the instructions for "Debugging While Running on ASP.NET Development Server", setting in web.config and the System.Diagnostics.Debugger.Break() statement at the start of the method. However, I executed with the "Debug Checked Tests" command (Ctrl+R, Ctrl+T) which produced a Just-in-time debugger prompts and ran up a new instance of Visual Studio (I'm using VS2010 Beta2). It worked well.



回答6:

Like Will mentioned, you can use test lists for all kinds of "test herding" scenarios, as each unit test in a solution can be a member of zero-or-more test lists and you can create a tree of test lists in Test -> Windows -> Test List Editor.