How to debug unittest failures in Visual C# 2008? - c#

This is nearly my first experience with unit testing.
I added a unittest to my solution, and selected Test->Run->All Tests in Solution. My test failed due to an exception which got thrown in the tested code.
Normally, I would then go to the stacktrace toolwindow, click my way through it, looking at the values of locals in every stackframe, and figure out what went wrong. But when code fails within an unittest, I don't get the normal "yellow balloon" exception notification, and I'm not able to explore the stacktrace in detail. All I get is a "TestMethod1 [Results]" tab, which displays only the exception message and a plaintext stacktrace. So, no access to the values of locals, no access to any debug-output I may have printed to the console...
How am I supposed to debug it then?

You need to select "Test->Debug->All tests in solution" then the debugger works as normal.
All the normal debug windows are available by going to "Debug->Windows".

You can install TestDriven.NET, which is a Visual Studio add-in that allows you to do just that - debug your tests. There is a free community version.

You can put a breakpoint in your code, like this:
<TestMethod> _
Public Sub Test() <--- Put breakpoint here.
and then choose to debug the unit test, you can then step through the code.

Related

Exception thrown in OneTimeTearDown; Tests not marked as failure

Title says it all ...
I'm working in C#, in Visual Studio, with NUnit, and with ReSharper as my TestRunner
I have a unit test with a [OneTimeTearDown] method.
That method is throwing an exception at the moment.
The test appears to be marked as 'inconclusive', rather than failed.
This seems a bit rubbish :(
Is there a way to fix this, or is it just how the framework works?
It's a combination of how the framework works and how the runner is reporting the result. At the point in time when OneTimeTearDown fails, all the tests have already been reported to the runner as succeeding, through test completion events.
So, those tests did run successfully but something went wrong in cleaning up the fixture. That error is reported against the fixture. Some runners may show this information and some may not. If you are running under the Test Explorer in Visual Studio, you'll notice that there is no info shown for fixtures, only for individual tests. So the runner, if it wants to report the failure to you, has no place to do it except possibly as text in the output window.
As an experiment, you might try running your tests under nunit3-console to see how it handles the result. You could also try using the NUnit 3 VS adapter without ReSharper to see how that comes out. Then pick the approach you like best and/or file an issue with the developers of the particular runner.
PS: If you run under nunit3-console, you can examine the XML result file to see what info is reported to any runner.
Sorry that this is not a more immediately useful answer!

VisualStudio 2017 - always show exceptions

I have a Visual Studio App (Windows.Forms) that build a release .exe-App. If I run this exe an exception occurs, I only can see it in the windows log, nothing more happens.
But I would like to see the JIT Exception Window. Can I activate this somehow in VS?
Im asking for a general "If any exception at runtime - show the JIT Debugger"-switch. Is something like this available?
Did it now like here shown.
http://www.csharp-examples.net/catching-unhandled-exceptions/
Instead of logging made an MessageBox with the Exception Message und Application.Exit(). Works exactly as i like it to have.

Disable Console output of all Handled/Caught Exceptions in Visual Studio

I'm currently using a library that throws (and handles) about 5 exceptions when a request to get something fails. Normally this isn't a problem since it's expected this might happen, but the problem is that Visual Studio will log these exceptions anyway.
Is there a way to disable Visual Studio from outputting caught exceptions to the debug console? I still want all other exceptions that would cause a break to be logged.
You can ask Visual Studio to care about the exceptions in your code only, using the Just My Code option.
Go to the "Debug" menu and click on "Options and Settings", and then Enable Just My Code:
This has been discussed on StackOverflow too.
Then, if the methods that are throwing the exception belong to a project that is... your code, you can decorate them with the DebuggerNonUserCode attribute:
which combined with the "Just My Code" option will produce the desired behaviour.
More on the topic here.

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?
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.
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.
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!
In visual studio 2017, make sure that solution configuration is under 'Debug' mode. Under 'Release' mode it is not debugging.
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.
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 ;)
Update for 2020:
If you're using Visual Studio Code with OmniSharp, you can click the "Debug Test" text above the method.
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!
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.
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
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"

Visual Studio - test with the same id already exists

I have a Class Library project in Visual Studio and tests for it (everything is written in C#). Once after successful solution build VS shows me this window:
I haven't seen this one before, and I don't know even from what to begin: project compiles without errors and works after compilation, but this window keep appearing after each compilation. When I press Retry or Abort, error message with the same text appears in Output window of VS.
There is only one test method named AddRange1201.
So, the question is - how to get rid of this, besides just checking "Do not show this dialog box again"?
I found this on MS Connect, and I'm not sure if it will fix your problem or not. Sounds like it's a known issue and occurs if you either add the test project to your solution twice (VS doesn't realize it's been added and removed) or if you switch between debug and release modes.
A workaround given is:
1) clean solutions in both modes
2) rebuild in the mode you want
Hopefully this helps.

Categories

Resources