how to debug with xUnit?

25,413

Solution 1

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.

Solution 2

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

Solution 3

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!

Solution 4

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

Solution 5

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

Share:
25,413

Related videos on Youtube

chronodekar
Author by

chronodekar

Looking for work as a web developer in the Toronto area.

Updated on April 21, 2022

Comments

  • chronodekar
    chronodekar about 2 years

    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?

    • J0HN
      J0HN almost 12 years
      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.
    • chronodekar
      chronodekar almost 12 years
      @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)?
    • gerryLowry
      gerryLowry almost 8 years
      @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.
  • adrianbanks
    adrianbanks almost 12 years
    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.
  • chronodekar
    chronodekar almost 12 years
    @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!! :)
  • Daniel
    Daniel almost 10 years
    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
  • STO
    STO almost 9 years
    This is unfortunately seems the only way to debug dnx test in released VS 2015
  • gerryLowry
    gerryLowry almost 8 years
    +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
    gerryLowry almost 8 years
    +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.
  • Kevin Scheidt
    Kevin Scheidt over 7 years
    I have tried this, but I keep getting TestClass.cs not found.
  • Vin Shahrdar
    Vin Shahrdar almost 3 years
    Is this a cyberpunk theme? Looks pretty neat
  • Brian Reading
    Brian Reading almost 3 years
    @VinShahrdar It's Synthwave x Fluoromachine