'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,
- 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?
Solution 1:[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:[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.
Solution 3:[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:[4]
In visual studio 2017, make sure that solution configuration is under 'Debug' mode. Under 'Release' mode it is not debugging.
Solution 5:[5]
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.
Solution 6:[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 ;)
Solution 7:[7]
Update for 2020: If you're using Visual Studio Code with OmniSharp, you can click the "Debug Test" text above the method.
Solution 8:[8]
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!
Solution 9:[9]
- set a break point inside of your method.
- from visual studio Menu bar, click on Test.
- from Debug click on Selected Test or All Test.
Solution 10:[10]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow