private bool SetTestContext(object classInstance, TestResult result) { Debug.Assert(classInstance != null, "classInstance != null"); Debug.Assert(result != null, "result != null"); try { if (this.Parent.TestContextProperty != null && this.Parent.TestContextProperty.CanWrite) { this.Parent.TestContextProperty.SetValue(classInstance, this.testContext); } return(true); } catch (Exception ex) { var stackTraceInfo = StackTraceHelper.GetStackTraceInformation(ex.GetInnerExceptionOrDefault()); var errorMessage = string.Format( CultureInfo.CurrentCulture, Resource.UTA_TestContextSetError, this.TestClassName, StackTraceHelper.GetExceptionMessage(ex.GetInnerExceptionOrDefault())); result.Outcome = TestTools.UnitTesting.UnitTestOutcome.Failed; result.TestFailureException = new TestFailedException(UnitTestOutcome.Failed, errorMessage, stackTraceInfo); } return(false); }
private object CreateTestClassInstance(TestResult result) { object classInstance = null; try { classInstance = this.Parent.Constructor.Invoke(null); } catch (Exception ex) { // In most cases, exception will be TargetInvocationException with real exception wrapped // in the InnerException; or user code throws an exception var actualException = ex.InnerException ?? ex; var exceptionMessage = StackTraceHelper.GetExceptionMessage(actualException); var stackTraceInfo = StackTraceHelper.GetStackTraceInformation(actualException); var errorMessage = string.Format( CultureInfo.CurrentCulture, Resource.UTA_InstanceCreationError, this.TestClassName, exceptionMessage); result.Outcome = TestTools.UnitTesting.UnitTestOutcome.Failed; result.TestFailureException = new TestFailedException(UnitTestOutcome.Failed, errorMessage, stackTraceInfo); } return(classInstance); }
/// <summary> /// Handles the exception that is thrown by a test method. The exception can either /// be expected or not expected /// </summary> /// <param name="ex">Exception that was thrown</param> /// <param name="className">The class name.</param> /// <param name="methodName">The method name.</param> /// <returns>Test framework exception with details.</returns> private Exception HandleMethodException(Exception ex, string className, string methodName) { Debug.Assert(ex != null, "exception should not be null."); var isTargetInvocationException = ex is TargetInvocationException; if (isTargetInvocationException && ex.InnerException == null) { var errorMessage = string.Format(CultureInfo.CurrentCulture, Resource.UTA_FailedToGetTestMethodException, className, methodName); return(new TestFailedException(UnitTestOutcome.Error, errorMessage)); } // Get the real exception thrown by the test method Exception realException = this.GetRealException(ex); if (realException is UnitTestAssertException) { return(new TestFailedException( realException is AssertInconclusiveException ? UnitTestOutcome.Inconclusive : UnitTestOutcome.Failed, realException.TryGetMessage(), realException.TryGetStackTraceInformation(), realException)); } else { string errorMessage; // Handle special case of UI objects in TestMethod to suggest UITestMethod if (realException.HResult == -2147417842) { errorMessage = string.Format( CultureInfo.CurrentCulture, Resource.UTA_WrongThread, string.Format(CultureInfo.CurrentCulture, Resource.UTA_TestMethodThrows, className, methodName, StackTraceHelper.GetExceptionMessage(realException))); } else { errorMessage = string.Format( CultureInfo.CurrentCulture, Resource.UTA_TestMethodThrows, className, methodName, StackTraceHelper.GetExceptionMessage(realException)); } StackTraceInformation stackTrace = null; // For ThreadAbortException (that can be thrown only by aborting a thread as there's no public constructor) // there's no inner exception and exception itself contains reflection-related stack trace // (_RuntimeMethodHandle.InvokeMethodFast <- _RuntimeMethodHandle.Invoke <- UnitTestExecuter.RunTestMethod) // which has no meaningful info for the user. Thus, we do not show call stack for ThreadAbortException. if (realException.GetType().Name != "ThreadAbortException") { stackTrace = StackTraceHelper.GetStackTraceInformation(realException); } return(new TestFailedException(UnitTestOutcome.Failed, errorMessage, stackTrace, realException)); } }
private bool RunTestInitializeMethod(object classInstance, TestResult result) { Debug.Assert(classInstance != null, "classInstance != null"); Debug.Assert(result != null, "result != null"); MethodInfo testInitializeMethod = null; try { // TestInitialize methods for base classes are called in reverse order of discovery // Grandparent -> Parent -> Child TestClass var baseTestInitializeStack = new Stack <MethodInfo>(this.Parent.BaseTestInitializeMethodsQueue); while (baseTestInitializeStack.Count > 0) { testInitializeMethod = baseTestInitializeStack.Pop(); testInitializeMethod?.InvokeAsSynchronousTask(classInstance, null); } testInitializeMethod = this.Parent.TestInitializeMethod; testInitializeMethod?.InvokeAsSynchronousTask(classInstance, null); return(true); } catch (Exception ex) { var innerException = ex.GetInnerExceptionOrDefault(); string exceptionMessage = null; StackTraceInformation exceptionStackTraceInfo = null; var outcome = TestTools.UnitTesting.UnitTestOutcome.Failed; if (innerException.TryGetUnitTestAssertException(out outcome, out exceptionMessage, out exceptionStackTraceInfo)) { result.Outcome = outcome; result.TestFailureException = new TestFailedException( UnitTestOutcome.Failed, exceptionMessage, exceptionStackTraceInfo); } else { var stackTrace = StackTraceHelper.GetStackTraceInformation(innerException); var errorMessage = string.Format( CultureInfo.CurrentCulture, Resource.UTA_InitMethodThrows, this.TestClassName, testInitializeMethod?.Name, StackTraceHelper.GetExceptionMessage(innerException)); result.Outcome = TestTools.UnitTesting.UnitTestOutcome.Failed; result.TestFailureException = new TestFailedException(UnitTestOutcome.Failed, errorMessage, stackTrace); } } return(false); }
public string RunClassCleanup() { if (this.ClassCleanupMethod is null && !this.BaseClassInitAndCleanupMethods.Any(p => p.Item2 != null)) { 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); } return(null); } catch (Exception exception) { var realException = exception.InnerException ?? exception; 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); } return(string.Format( CultureInfo.CurrentCulture, Resource.UTA_ClassCleanupMethodWasUnsuccesful, classCleanupMethod.DeclaringType.Name, classCleanupMethod.Name, errorMessage, StackTraceHelper.GetStackTraceInformation(realException)?.ErrorStackTrace)); } } return(null); }
public string RunClassCleanup() { if (this.ClassCleanupMethod == null) { return(null); } if (this.IsClassInitializeExecuted || this.ClassInitializeMethod == null) { try { this.ClassCleanupMethod.InvokeAsSynchronousTask(null); return(null); } catch (Exception exception) { var realException = exception.InnerException ?? exception; 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); } return(string.Format( CultureInfo.CurrentCulture, Resource.UTA_ClassCleanupMethodWasUnsuccesful, this.ClassType.Name, this.ClassCleanupMethod.Name, errorMessage, StackTraceHelper.GetStackTraceInformation(realException)?.ErrorStackTrace)); } } return(null); }
/// <summary> /// Create stack trace information /// </summary> /// <param name="ex"> /// The exception. /// </param> /// <param name="checkInnerExceptions"> /// Whether the inner exception needs to be checked too. /// </param> /// <param name="stackTraceString"> /// The stack Trace String. /// </param> /// <returns> /// The <see cref="StackTraceInformation"/>. /// </returns> internal static StackTraceInformation CreateStackTraceInformation( Exception ex, bool checkInnerExceptions, string stackTraceString) { if (checkInnerExceptions && ex.InnerException != null) { return(CreateStackTraceInformation(ex.InnerException, checkInnerExceptions, stackTraceString)); } var stackTrace = StackTraceHelper.TrimStackTrace(stackTraceString); if (!string.IsNullOrEmpty(stackTrace)) { return(new StackTraceInformation(stackTrace, null, 0, 0)); } return(null); }
private string GetTestCleanUpExceptionMessage(MethodInfo testCleanupMethod, Exception exception) { if (testCleanupMethod != null) { return(string.Format( CultureInfo.CurrentCulture, Resource.UTA_CleanupMethodThrows, this.TestClassName, testCleanupMethod?.Name, exception.GetType().ToString(), StackTraceHelper.GetExceptionMessage(exception))); } else { return(string.Format( CultureInfo.CurrentCulture, Resource.UTA_CleanupMethodThrowsGeneralError, this.TestClassName, StackTraceHelper.GetExceptionMessage(exception))); } }
private void RunTestCleanupMethod(object classInstance, TestResult result) { Debug.Assert(classInstance != null, "classInstance != null"); Debug.Assert(result != null, "result != null"); var testCleanupMethod = this.Parent.TestCleanupMethod; try { try { // Test cleanups are called in the order of discovery // Current TestClass -> Parent -> Grandparent testCleanupMethod?.InvokeAsSynchronousTask(classInstance, null); var baseTestCleanupQueue = new Queue <MethodInfo>(this.Parent.BaseTestCleanupMethodsQueue); while (baseTestCleanupQueue.Count > 0) { testCleanupMethod = baseTestCleanupQueue.Dequeue(); testCleanupMethod?.InvokeAsSynchronousTask(classInstance, null); } } finally { (classInstance as IDisposable)?.Dispose(); } } catch (Exception ex) { var stackTraceInformation = StackTraceHelper.GetStackTraceInformation(ex.GetInnerExceptionOrDefault()); var errorMessage = string.Format( CultureInfo.CurrentCulture, Resource.UTA_CleanupMethodThrows, this.TestClassName, testCleanupMethod?.Name, StackTraceHelper.GetExceptionMessage(ex.GetInnerExceptionOrDefault())); result.Outcome = TestTools.UnitTesting.UnitTestOutcome.Failed; result.TestFailureException = new TestFailedException(UnitTestOutcome.Failed, errorMessage, stackTraceInformation); } }
public void RunAssemblyInitialize(TestContext testContext) { // No assembly initialize => nothing to do. if (this.AssemblyInitializeMethod == null) { return; } if (testContext == null) { throw new NullReferenceException(Resource.TestContextIsNull); } // If assembly initialization is not done, then do it. if (!this.IsAssemblyInitializeExecuted) { // Acquiring a lock is usually a costly operation which does not need to be // performed every time if the assembly init is already executed. lock (this.assemblyInfoExecuteSyncObject) { // Perform a check again. if (!this.IsAssemblyInitializeExecuted) { try { this.AssemblyInitializeMethod.InvokeAsSynchronousTask(null, testContext); } catch (Exception ex) { this.AssemblyInitializationException = ex; } finally { this.IsAssemblyInitializeExecuted = true; } } } } // If assemblyInitialization was successful, then don't do anything if (this.AssemblyInitializationException == null) { return; } // Cache and return an already created TestFailedException. if (this.AssemblyInitializationException is TestFailedException) { throw this.AssemblyInitializationException; } var realException = this.AssemblyInitializationException.InnerException ?? this.AssemblyInitializationException; var outcome = UnitTestOutcome.Failed; string errorMessage = null; StackTraceInformation stackTraceInfo = null; if (!realException.TryGetUnitTestAssertException(out outcome, out errorMessage, out 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); this.AssemblyInitializationException = testFailedException; throw testFailedException; }
/// <summary> /// Run class cleanup methods /// </summary> /// <param name="classCleanupLifecycle">The current lifecyle position that ClassCleanup is executing from</param> /// <returns> /// Any exception that can be thrown as part of a class cleanup as warning messages. /// </returns> 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); }
public void RunClassInitialize(TestContext testContext) { // If no class initialize and no base class initialize, return if (this.ClassInitializeMethod is null && !this.BaseClassInitAndCleanupMethods.Any(p => p.Item1 != null)) { return; } if (testContext == null) { throw new NullReferenceException(Resource.TestContextIsNull); } MethodInfo initializeMethod = null; string failedClassInitializeMethodName = string.Empty; // If class initialization is not done, then do it. if (!this.IsClassInitializeExecuted) { // Aquiring a lock is usually a costly operation which does not need to be // performed every time if the class init is already executed. lock (this.testClassExecuteSyncObject) { // Perform a check again. if (!this.IsClassInitializeExecuted) { try { // ClassInitialize methods for base classes are called in reverse order of discovery // Base -> Child TestClass var baseClassInitializeStack = new Stack <Tuple <MethodInfo, MethodInfo> >( this.BaseClassInitAndCleanupMethods.Where(p => p.Item1 != null)); while (baseClassInitializeStack.Count > 0) { var baseInitCleanupMethods = baseClassInitializeStack.Pop(); initializeMethod = baseInitCleanupMethods.Item1; initializeMethod?.InvokeAsSynchronousTask(null, testContext); if (baseInitCleanupMethods.Item2 != null) { this.BaseClassCleanupMethodsStack.Push(baseInitCleanupMethods.Item2); } } initializeMethod = null; if (this.classInitializeMethod != null) { this.ClassInitializeMethod.InvokeAsSynchronousTask(null, testContext); } } catch (Exception ex) { this.ClassInitializationException = ex; failedClassInitializeMethodName = initializeMethod?.Name ?? this.ClassInitializeMethod.Name; } finally { this.IsClassInitializeExecuted = true; } } } } // If classInitialization was successful, then don't do anything if (this.ClassInitializationException == null) { return; } if (this.ClassInitializationException is TestFailedException) { throw this.ClassInitializationException; } // 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 testFailedException = new TestFailedException(outcome, errorMessage, exceptionStackTraceInfo); this.ClassInitializationException = testFailedException; throw testFailedException; }
public void RunClassInitialize(TestContext testContext) { // If no class initialize return if (this.ClassInitializeMethod == null) { return; } if (testContext == null) { throw new NullReferenceException(Resource.TestContextIsNull); } // If class initialization is not done, then do it. if (!this.IsClassInitializeExecuted) { try { this.ClassInitializeMethod.InvokeAsSynchronousTask(null, testContext); } catch (Exception ex) { this.ClassInitializationException = ex; } finally { this.IsClassInitializeExecuted = true; } } // If classInitialization was successful, then dont do anything if (this.ClassInitializationException == null) { return; } if (this.ClassInitializationException is TestFailedException) { throw this.ClassInitializationException; } // Fail the current test if it was a failure. var realException = this.ClassInitializationException.InnerException ?? this.ClassInitializationException; var outcome = UnitTestOutcome.Failed; string errorMessage = null; StackTraceInformation exceptionStackTraceInfo = null; if (!realException.TryGetUnitTestAssertException(out outcome, out errorMessage, out exceptionStackTraceInfo)) { errorMessage = string.Format( CultureInfo.CurrentCulture, Resource.UTA_ClassInitMethodThrows, this.ClassType.FullName, this.ClassInitializeMethod.Name, realException.GetType().ToString(), StackTraceHelper.GetExceptionMessage(realException)); exceptionStackTraceInfo = realException.TryGetStackTraceInformation(); } var testFailedException = new TestFailedException(outcome, errorMessage, exceptionStackTraceInfo); this.ClassInitializationException = testFailedException; throw testFailedException; }
public void RunAssemblyInitialize(TestContext testContext) { // No assembly initialize => nothing to do. if (this.AssemblyInitializeMethod == null) { return; } if (testContext == null) { throw new NullReferenceException(Resource.TestContextIsNull); } // If assembly initialization is not done, then do it. if (!this.IsAssemblyInitializeExecuted) { try { this.AssemblyInitializeMethod.InvokeAsSynchronousTask(null, testContext); } catch (Exception ex) { this.AssemblyInitializationException = ex; } finally { this.IsAssemblyInitializeExecuted = true; } } // If assemblyInitialization was successful, then dont do anything if (this.AssemblyInitializationException == null) { return; } // Cache and return an already created TestFailedException. if (this.AssemblyInitializationException is TestFailedException) { throw this.AssemblyInitializationException; } var realException = this.AssemblyInitializationException.InnerException ?? this.AssemblyInitializationException; var outcome = UnitTestOutcome.Failed; string errorMessage = null; StackTraceInformation stackTraceInfo = null; if (realException is UnitTestAssertException) { outcome = realException is AssertInconclusiveException ? UnitTestOutcome.Inconclusive : UnitTestOutcome.Failed; errorMessage = realException.TryGetMessage(); stackTraceInfo = realException.TryGetStackTraceInformation(); } else { 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); this.AssemblyInitializationException = testFailedException; throw testFailedException; }