/// <summary> /// Invokes the test method on the given test class instance. /// </summary> /// <param name="testClassInstance">The test class instance</param> /// <returns>Returns the time taken to invoke the test method</returns> public virtual async Task <decimal> InvokeTestMethodAsync(object testClassInstance) { var oldSyncContext = SynchronizationContext.Current; try { var asyncSyncContext = new AsyncTestSyncContext(oldSyncContext); SetSynchronizationContext(asyncSyncContext); await Aggregator.RunAsync( () => Timer.AggregateAsync( async() => { var parameterCount = TestMethod.GetParameters().Length; var valueCount = TestMethodArguments == null ? 0 : TestMethodArguments.Length; if (parameterCount != valueCount) { Aggregator.Add( new InvalidOperationException( String.Format("The test method expected {0} parameter value{1}, but {2} parameter value{3} {4} provided.", parameterCount, parameterCount == 1 ? "" : "s", valueCount, valueCount == 1 ? "" : "s", valueCount == 1 ? "was" : "were")) ); } else { var result = TestMethod.Invoke(testClassInstance, TestMethodArguments); var task = result as Task; if (task != null) { await task; } else { var ex = await asyncSyncContext.WaitForCompletionAsync(); if (ex != null) { Aggregator.Add(ex); } } } } ) ); } finally { SetSynchronizationContext(oldSyncContext); } return(Timer.Total); }
/// <summary> /// Invokes the test method on the given test class instance. /// </summary> /// <param name="testClassInstance">The test class instance</param> /// <returns>Returns the time taken to invoke the test method</returns> public virtual async Task <decimal> InvokeTestMethodAsync(object testClassInstance) { var oldSyncContext = SynchronizationContext.Current; try { var asyncSyncContext = new AsyncTestSyncContext(oldSyncContext); SetSynchronizationContext(asyncSyncContext); await Aggregator.RunAsync( () => Timer.AggregateAsync( async() => { var result = TestMethod.Invoke(testClassInstance, TestMethodArguments); var task = result as Task; if (task != null) { await task; } else { var ex = await asyncSyncContext.WaitForCompletionAsync(); if (ex != null) { Aggregator.Add(ex); } } } ) ); } finally { SetSynchronizationContext(oldSyncContext); } return(Timer.Total); }
/// <summary> /// For unit testing purposes only. /// </summary> protected virtual async Task InvokeTestMethodAsync(object testClassInstance) { var oldSyncContext = SynchronizationContext.Current; try { var asyncSyncContext = new AsyncTestSyncContext(); SetSynchronizationContext(asyncSyncContext); await Aggregator.RunAsync( () => Timer.AggregateAsync( async() => { var result = TestMethod.Invoke(testClassInstance, TestMethodArguments); var task = result as Task; if (task != null) { await task.ConfigureAwait(false); } else { var ex = await asyncSyncContext.WaitForCompletionAsync().ConfigureAwait(false); if (ex != null) { Aggregator.Add(ex); } } } ) ); // Don't use configure await false here, as we need to be on the orig context here to reset in the finally } finally { SetSynchronizationContext(oldSyncContext); } }
/// <summary> /// This method calls the test method via reflection. This is an available override point /// if you need to do some other form of invocation of the actual test method. /// </summary> /// <param name="testClassInstance">The instance of the test class</param> /// <returns>The return value from the test method invocation</returns> protected virtual object CallTestMethod(object testClassInstance) => TestMethod.Invoke(testClassInstance, TestMethodArguments);