chronodekar
chronodekar

Reputation: 2746

how to debug with xUnit?

I'm learning xUnit and so far, have found it to be a most useful tool. It's making me rethink some of my coding tactics to TDD instead.

However, I've come across an interesting problem. My test case is failing. No real concern there, but how do I debug it?

Specifically my test case is failing due to a "out of index" error, or something similar. It's NOT failing at the assert statement. What I need now, is some way to run the test case with the Visual Studio debugger active so that I can see the status of the different variables.

I'm not going to post code, as this situation is bound to come up again. Does anyone have any idea HOW to debug the test case itself?

Almost forgot! I'm using,

My workflow involves using the xUnit GUI runner to run the tests.

If what I'm asking is impossible, can someone suggest an alternative test suite I could use that has what I want?

Upvotes: 54

Views: 34064

Answers (11)

Vala Khosravi
Vala Khosravi

Reputation: 2570

Here's what I did, I just set the breakpoints, and from the menu from the top I choose "Test > Debug All Tests" or "Test > Debug last run"

enter image description here

Upvotes: 0

bbsimonbb
bbsimonbb

Reputation: 29020

In VS2015 and later, install the xunit.runner.visualstudio NuGet package. Then debugging is as easy as right-clicking on the test in the test explorer window. (Test-->Windows-->TestExplorer if you can't see it). You can also right-click anywhere in the code of the test and Run Test and Debug Test will be in the context menu. If your test is not showing up, be sure the class and method are both public.

enter image description here

Upvotes: 87

PaulGrant
PaulGrant

Reputation: 394

I tried all of the above and had no success. The thing that worked for me is from the following post: https://github.com/OmniSharp/omnisharp-vscode/issues/1630#issuecomment-317797607

In the .csproj file, under PropertyGroup add the following: portable

Upvotes: 0

Brian Reading
Brian Reading

Reputation: 598

Update for 2020: If you're using Visual Studio Code with OmniSharp, you can click the "Debug Test" text above the method.

Visual Studio Code with OmniSharp and xUnit

Upvotes: 5

Zeek2
Zeek2

Reputation: 424

See the answer to this question: Stepping through and debugging code in Unit tests .

Essentially:

...go to 'Test' in the main menu of VS..., click submenu 'Debug' . . .'.

It also works for me using VS2017 ;)

Upvotes: 6

Mohammad Reza Mrg
Mohammad Reza Mrg

Reputation: 2043

  1. set a break point inside of your method.
  2. from visual studio Menu bar, click on Test.
  3. from Debug click on Selected Test or All Test.

Upvotes: 1

Megha
Megha

Reputation: 529

In visual studio 2017, make sure that solution configuration is under 'Debug' mode. Under 'Release' mode it is not debugging.

Upvotes: 7

Laurentiu Ilici
Laurentiu Ilici

Reputation: 146

I have failed in implementing all of the above, but the following worked for me: Before the lines where you want to debug add the following line (then run the test):

        System.Diagnostics.Debugger.Launch();

The drawback is that it will launch another instance of VS :).

Cheers!

Upvotes: 11

Adam Diament
Adam Diament

Reputation: 4880

If you have resharper, with X-unit contrib extension installed (seriously recommended!), right click the class in visual studio and click "debug unit tests".

Simple!

Upvotes: 2

Boggin
Boggin

Reputation: 3416

The following will work in VS.NET and in SharpDevelop.

Open the test project's properties and go to Debug tab:

  • Under "Start Action" set "Start external program" to the xUnit runner executable of choice.

  • Under "Start Options" set "Command line arguments" to the name of your project's debug DLL.

  • Also set "Working directory" to the project's "bin\Debug\" directory.

Then select Debug > Run or press F5 to run your test in debug mode. Breakpoints will be hit.

The advantage of doing your debugging this way is you don't have to attach to the xUnit GUI each time, you just need to run your test project.

Upvotes: 6

Dave
Dave

Reputation: 3621

I've not tested this but you should be able to attach visual studio to the xUnit GUI and debug from there.

From the Debug menu select 'attach to process', locate the name of the executable in the list and click attach. Set breakpoints in the unit test as required and run the test from the GUI. The breakpoint should be hit as expected.

Upvotes: 11

Related Questions