Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better messages for XXXInitialize and XXXCleanup #1147

Merged
merged 1 commit into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions src/Adapter/MSTest.CoreAdapter/Execution/TestAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,25 +169,22 @@ public void RunAssemblyInitialize(TestContext testContext)
throw this.AssemblyInitializationException;
}

var realException = this.AssemblyInitializationException.InnerException
?? this.AssemblyInitializationException;

var outcome = UnitTestOutcome.Failed;
if (!realException.TryGetUnitTestAssertException(out outcome, out var errorMessage, out var stackTraceInfo))
{
var exception = realException.GetType().ToString();
var message = StackTraceHelper.GetExceptionMessage(realException);
errorMessage = string.Format(
CultureInfo.CurrentCulture,
Resource.UTA_AssemblyInitMethodThrows,
this.AssemblyInitializeMethod.DeclaringType.FullName,
this.AssemblyInitializeMethod.Name,
exception,
message);
stackTraceInfo = StackTraceHelper.GetStackTraceInformation(realException);
}

var testFailedException = new TestFailedException(outcome, errorMessage, stackTraceInfo);
var realException = this.AssemblyInitializationException.InnerException ?? this.AssemblyInitializationException;

var outcome = realException is AssertInconclusiveException ? UnitTestOutcome.Inconclusive : UnitTestOutcome.Failed;

// Do not use StackTraceHelper.GetExceptionMessage(realException) as it prefixes the message with the exception type name.
var exceptionMessage = realException.TryGetMessage();
var errorMessage = string.Format(
CultureInfo.CurrentCulture,
Resource.UTA_AssemblyInitMethodThrows,
Haplois marked this conversation as resolved.
Show resolved Hide resolved
this.AssemblyInitializeMethod.DeclaringType.FullName,
this.AssemblyInitializeMethod.Name,
realException.GetType().ToString(),
exceptionMessage);
var exceptionStackTraceInfo = StackTraceHelper.GetStackTraceInformation(realException);

var testFailedException = new TestFailedException(outcome, errorMessage, exceptionStackTraceInfo, realException);
Evangelink marked this conversation as resolved.
Show resolved Hide resolved
this.AssemblyInitializationException = testFailedException;

throw testFailedException;
Expand Down Expand Up @@ -243,4 +240,4 @@ public string RunAssemblyCleanup()
}
}
}
}
}
35 changes: 18 additions & 17 deletions src/Adapter/MSTest.CoreAdapter/Execution/TestClassInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution
using System.Globalization;
using System.Linq;
using System.Reflection;

using Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ObjectModel;
using UnitTestOutcome = Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel.UnitTestOutcome;

using ObjectModelUnitTestOutcome = ObjectModel.UnitTestOutcome;

/// <summary>
/// Defines the TestClassInfo object
Expand Down Expand Up @@ -319,21 +321,20 @@ public void RunClassInitialize(TestContext testContext)
// Fail the current test if it was a failure.
var realException = this.ClassInitializationException.InnerException ?? this.ClassInitializationException;

var outcome = UnitTestOutcome.Failed;
if (!realException.TryGetUnitTestAssertException(out outcome, out string errorMessage, out StackTraceInformation exceptionStackTraceInfo))
{
errorMessage = string.Format(
CultureInfo.CurrentCulture,
Resource.UTA_ClassInitMethodThrows,
this.ClassType.FullName,
failedClassInitializeMethodName,
realException.GetType().ToString(),
StackTraceHelper.GetExceptionMessage(realException));

exceptionStackTraceInfo = realException.TryGetStackTraceInformation();
}
var outcome = realException is AssertInconclusiveException ? ObjectModelUnitTestOutcome.Inconclusive : ObjectModelUnitTestOutcome.Failed;

// Do not use StackTraceHelper.GetExceptionMessage(realException) as it prefixes the message with the exception type name.
var exceptionMessage = realException.TryGetMessage();
var errorMessage = string.Format(
CultureInfo.CurrentCulture,
Resource.UTA_ClassInitMethodThrows,
Haplois marked this conversation as resolved.
Show resolved Hide resolved
this.ClassType.FullName,
failedClassInitializeMethodName,
realException.GetType().ToString(),
exceptionMessage);
var exceptionStackTraceInfo = StackTraceHelper.GetStackTraceInformation(realException);

var testFailedException = new TestFailedException(outcome, errorMessage, exceptionStackTraceInfo);
var testFailedException = new TestFailedException(outcome, errorMessage, exceptionStackTraceInfo, realException);
Haplois marked this conversation as resolved.
Show resolved Hide resolved
this.ClassInitializationException = testFailedException;

throw testFailedException;
Expand Down Expand Up @@ -411,7 +412,7 @@ public string RunClassCleanup(ClassCleanupBehavior classCleanupLifecycle = Class

if (classCleanupLifecycle == ClassCleanupBehavior.EndOfClass)
{
var testFailedException = new TestFailedException(UnitTestOutcome.Failed, errorMessage, exceptionStackTraceInfo);
var testFailedException = new TestFailedException(ObjectModelUnitTestOutcome.Failed, errorMessage, exceptionStackTraceInfo);
this.ClassCleanupException = testFailedException;
throw testFailedException;
}
Expand All @@ -425,4 +426,4 @@ public string RunClassCleanup(ClassCleanupBehavior classCleanupLifecycle = Class
return null;
}
}
}
}
Loading