in src/Adapter/MSTest.CoreAdapter/Execution/TestClassInfo.cs [349:426]
public string RunClassCleanup(ClassCleanupBehavior classCleanupLifecycle = ClassCleanupBehavior.EndOfAssembly)
{
if (this.ClassCleanupMethod is null && this.BaseClassInitAndCleanupMethods.All(p => p.Item2 == null))
{
return null;
}
if (!this.IsClassCleanupExecuted)
{
lock (this.testClassExecuteSyncObject)
{
if (this.IsClassCleanupExecuted)
{
return null;
}
if (this.IsClassInitializeExecuted || this.ClassInitializeMethod is null)
{
MethodInfo classCleanupMethod = null;
try
{
classCleanupMethod = this.ClassCleanupMethod;
classCleanupMethod?.InvokeAsSynchronousTask(null);
var baseClassCleanupQueue = new Queue<MethodInfo>(this.BaseClassCleanupMethodsStack);
while (baseClassCleanupQueue.Count > 0)
{
classCleanupMethod = baseClassCleanupQueue.Dequeue();
classCleanupMethod?.InvokeAsSynchronousTask(null);
}
this.IsClassCleanupExecuted = true;
return null;
}
catch (Exception exception)
{
var realException = exception.InnerException ?? exception;
this.ClassCleanupException = realException;
string errorMessage;
// special case AssertFailedException to trim off part of the stack trace
if (realException is AssertFailedException ||
realException is AssertInconclusiveException)
{
errorMessage = realException.Message;
}
else
{
errorMessage = StackTraceHelper.GetExceptionMessage(realException);
}
var exceptionStackTraceInfo = realException.TryGetStackTraceInformation();
errorMessage = string.Format(
CultureInfo.CurrentCulture,
Resource.UTA_ClassCleanupMethodWasUnsuccesful,
classCleanupMethod.DeclaringType.Name,
classCleanupMethod.Name,
errorMessage,
exceptionStackTraceInfo?.ErrorStackTrace);
if (classCleanupLifecycle == ClassCleanupBehavior.EndOfClass)
{
var testFailedException = new TestFailedException(UnitTestOutcome.Failed, errorMessage, exceptionStackTraceInfo);
this.ClassCleanupException = testFailedException;
throw testFailedException;
}
return errorMessage;
}
}
}
}
return null;
}