-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathHandleApplicationThreadExceptionAttribute.cs
More file actions
52 lines (45 loc) · 1.74 KB
/
HandleApplicationThreadExceptionAttribute.cs
File metadata and controls
52 lines (45 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (c) 2017 SIL International
// This software is licensed under the LGPL, version 2.1 or later
// (http://www.gnu.org/licenses/lgpl-2.1.html)
using System;
using System.Threading;
using System.Windows.Forms;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
namespace SIL.FieldWorks.Common.FwUtils.Attributes
{
/// <summary>
/// Handles unhandled exceptions that occur in Windows Forms threads. This avoids the display of unhandled exception dialogs when
/// running nunit-console. Avoiding the dialogs is preferable, because it can cause an unattended build to pause, while it waits for
/// input from the user. In addition, if the user presses "Continue", it makes the test pass. Rethrowing the exception doesn't bring
/// up the dialog and correctly makes the test fail.
/// </summary>
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Method,
AllowMultiple = true)]
public class HandleApplicationThreadExceptionAttribute : TestActionAttribute
{
/// <summary>
/// Method called before each test
/// </summary>
public override void BeforeTest(ITest test)
{
base.BeforeTest(test);
Application.ThreadException += OnThreadException;
}
/// <summary>
/// Method called after each test
/// </summary>
public override void AfterTest(ITest test)
{
base.AfterTest(test);
Application.ThreadException -= OnThreadException;
}
private void OnThreadException(object sender, ThreadExceptionEventArgs e)
{
Console.Error.WriteLine("Unhandled Windows Forms thread exception during test run:");
Console.Error.WriteLine(e.Exception.ToString());
Console.Error.Flush();
throw new ApplicationException(e.Exception.Message, e.Exception);
}
}
}