54

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,

  • Visual Studio 2010 Ultimate (Dreamspark license)
  • xUnit 1.9

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?

3
  • 1
    Actually, if you are to debug unit test, it's a bad unit test. Tests should be designed as simple and straightforward as possible, so you can verify it's correctness by barely looking at them. Consider rewriting this test.
    – J0HN
    Commented Jun 26, 2012 at 10:49
  • 4
    @J0HN, the error in question came from the code I was testing, NOT the unit test. My problem was in finding WHY it was occurring. xUnit helpfully tells me that line XX produced exception YYY, but it doesn't tell me anything else. Like what was the state of the variables when the exception occurred (or the like)? Commented Jun 26, 2012 at 12:42
  • @J0HN there are cases when the unit test is not bad per se; I just had an example which was very difficult to discover because of cognitive dissonance ~~ caveat, this occurred very early in the development of a project and was related to a numeric literal in the SUT that had transposed digits; in this case the unit test + debug collaborated to help me make the SUT more robust.
    – gerryLowry
    Commented Aug 7, 2016 at 19:49

11 Answers 11

87

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

2
  • 1
    +1 @user1585345 thank you ... i'd forgotten the "when in doubt, right-click" mantra; your suggestion is a wonderful way to quickly debug a single failing test should that be necessary.
    – gerryLowry
    Commented Aug 7, 2016 at 19:58
  • 2
    I have tried this, but I keep getting TestClass.cs not found. Commented Nov 4, 2016 at 18:19
11

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.

2
  • 1
    With NUnit, you can set the GUI as the program to start when debugging and pass the assembly name as an argument. Hitting F5 will then launch the NUnit GUI under the debugger with the correct assembly. I'm assuming that you can do something similar with xUnit. Commented Jun 26, 2012 at 11:36
  • @adrianbanks Thank you VERY much for that idea! Never really occurred to me. Some twiddling around with the "debug" properties and I got the xUnit GUI runner to start up when I run debug AND with variable info too!! :) Commented Jun 26, 2012 at 12:49
11

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!

1
  • This is unfortunately seems the only way to debug dnx test in released VS 2015
    – STO
    Commented Jul 26, 2015 at 10:33
7

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

6

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.

6

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 ;)

5

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

3
  • 1
    Is this a cyberpunk theme? Looks pretty neat Commented Aug 13, 2021 at 15:17
  • @VinShahrdar It's Synthwave x Fluoromachine Commented Aug 14, 2021 at 0:54
  • Is there a way to debug all tests in all classes of a test project?
    – Terry
    Commented Mar 2 at 20:48
2

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!

2
  • As long as you don't use the extension for ReSharper that should support xUnit2, then debugging is a pain in the.... github.com/xunit/resharper-xunit/issues/9
    – Daniel
    Commented Jul 29, 2014 at 18:50
  • +1 @Adam Diment ... thank you ... i'm using vs2015 Enterprise with JetBrains ReSharper Ultimate so I'm not sure whether this feature is from ReSharper or now native to vs2015; regardless, on right-click the class name of my xUnit.net test class, because of your guidance, i found "Debug Tests" (ctrl+r, ctrl+t) ... your suggestion helped me discover the reason my test was failing (specially a typo regarding a literal to which i'd been blinded). B-)
    – gerryLowry
    Commented Aug 7, 2016 at 19:42
1
  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.
0

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.