UnhandledExceptionEventHandler in MSTest doesn't show the exception
See original GitHub issueDescription
Using the UnhandledExceptionEventHandler in MSTest doesn’t show any exception being caught.
Steps to reproduce
- Create a simple UnitTest project which could be either MSTest (.NET core) or UnitTest Project(.NET Framework)
- Attach an UnhandledException event handler to the AppDomain
- Create any kind of null reference or an InvalidOperation Exception and try to add it to your logger or Console Write the caught exception.
[TestClass]
public class UnitTest1
{
[AssemblyInitialize]
public static void AssemblyInitialize(TestContext testContext)
{
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += currentDomain_UnhandledException;
}
static void currentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Exception ex = e.ExceptionObject as Exception;
if (ex != null)
{
Console.WriteLine("==========This is from Unhandled Exception EventHandler====================");
Console.WriteLine("===============================");
Console.WriteLine("Test failed:" + ex);
}
}
[TestInitialize]
public void Initialize()
{
}
[TestCleanup]
public void Cleanup()
{
}
[TestMethod]
public void RaiseEvents()
{
var list = new List<string>(); //empty list
var firstItem = list.First().Contains("a");
}
[AssemblyCleanup]
public static void AssemblyCleanUp()
{
}
}
Expected behavior
Should display the Unhandled exception
Actual behavior
Nothing is displayed in the output window under TestExplorer
Environment
- Window 10 Pro
- Build version of vstest.console: 15.9.1
- Microsoft.NET.Sdk 16.4.0
- MSTest.TestAdapter 2.0.0
- MSTest.TestFramework 2.0.0
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:10 (5 by maintainers)
Top Results From Across the Web
How to use UnhandledExceptionEventHandler in MSTest ...
I have a simple class as below. I'm unable to print the exceptions for the below unit test using the UnhandledExceptionEventHandler.
Read more >Not able to Catch unhandled exception - Microsoft Q&A
In my application I want to log/display the details of any unhandled exception occurred . So I used following code to handle the...
Read more >What is an Unhandled Exception, and How to Catch All C# ...
An exception is a known type of error. An unhandled exception occurs when the application code does not properly handle exceptions.
Read more >Mastering Lob Development for Silverlight 5: A Case Study in ...
... feature / Testing client code with MSTest - AsyncCallSimulator/ Packt. ... UnhandledExceptionEventHandler/ ExceptionRouted EventArgsZUnhandled Exception ...
Read more >[Solved]-c# unhandled exceptions catcher-C#
I call a method in the Program.cs file to set an un-handled exception method. Here is the full explanation of Application.SetUnhandledExceptionMode Method ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Below is is just one of the example scenarios. I’m using MSTest for selenium :
In some of my tests I’m expecting that a collection contains some Ui elements(like dropdown values) but due to some functionality or due to some test data, collection will be either empty or it doesn’t contain the required data. In such cases when I’m trying to access the elements MSTest Platform throws Invalid Operation exception. Control or flow of execution goes directly to TestCleanup. When I wanted to access/capture the exception for my reports. I do not have any mechanism to capture such exceptions. TestContext has a property which tells me only the outcome of the test but it doesn’t give any exception information.
If you have the same scenario in some console projects you can capture the exceptions but the same is not available in MSTest.
This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 10 days of this comment.