/// <summary> /// OnMethodBegin callback /// </summary> /// <typeparam name="TTarget">Type of the target</typeparam> /// <typeparam name="TContext">ExecutionContext type</typeparam> /// <param name="instance">Instance value, aka `this` of the instrumented method.</param> /// <param name="executionContext">Execution context instance</param> /// <returns>Calltarget state value</returns> internal static CallTargetState OnMethodBegin <TTarget, TContext>(TTarget instance, TContext executionContext) where TContext : ITestExecutionContext { if (!NUnitIntegration.IsEnabled) { return(CallTargetState.GetDefault()); } return(new CallTargetState(NUnitIntegration.CreateScope(executionContext.CurrentTest, typeof(TTarget)))); }
/// <summary> /// OnMethodBegin callback /// </summary> /// <typeparam name="TTarget">Type of the target</typeparam> /// <typeparam name="TContext">ExecutionContext type</typeparam> /// <param name="instance">Instance value, aka `this` of the instrumented method.</param> /// <param name="executionContext">Execution context instance</param> /// <returns>Calltarget state value</returns> public static CallTargetState OnMethodBegin <TTarget, TContext>(TTarget instance, TContext executionContext) where TContext : ITestExecutionContext { if (!Common.TestTracer.Settings.IsIntegrationEnabled(IntegrationId)) { return(CallTargetState.GetDefault()); } return(new CallTargetState(NUnitIntegration.CreateScope(executionContext, typeof(TTarget)))); }
/// <summary> /// OnMethodEnd callback /// </summary> /// <typeparam name="TTarget">Type of the target</typeparam> /// <typeparam name="TResult">TestResult type</typeparam> /// <param name="instance">Instance value, aka `this` of the instrumented method.</param> /// <param name="returnValue">Original method return value</param> /// <param name="exception">Exception instance in case the original code threw an exception.</param> /// <param name="state">Calltarget state value</param> /// <returns>Return value of the method</returns> public static CallTargetReturn <TResult> OnMethodEnd <TTarget, TResult>(TTarget instance, TResult returnValue, Exception exception, CallTargetState state) { Scope scope = state.Scope; if (scope != null) { NUnitIntegration.FinishScope(scope, exception); scope.Dispose(); } return(new CallTargetReturn <TResult>(returnValue)); }
/// <summary> /// OnMethodEnd callback /// </summary> /// <typeparam name="TTarget">Type of the target</typeparam> /// <param name="instance">Instance value, aka `this` of the instrumented method.</param> /// <param name="exception">Exception instance in case the original code threw an exception.</param> /// <param name="state">Calltarget state value</param> /// <returns>Return value of the method</returns> public static CallTargetReturn OnMethodEnd <TTarget>(TTarget instance, Exception exception, CallTargetState state) { if (state.State != null) { object[] stateArray = (object[])state.State; string skipMessage = (string)stateArray[1]; const string startString = "OneTimeSetUp:"; if (skipMessage?.StartsWith(startString, StringComparison.OrdinalIgnoreCase) == true) { skipMessage = skipMessage.Substring(startString.Length).Trim(); } object testSuiteOrWorkItem = stateArray[0]; if (testSuiteOrWorkItem is not null) { string typeName = testSuiteOrWorkItem.GetType().Name; if (typeName == "ParameterizedMethodSuite") { // In case the TestSuite is a ParameterizedMethodSuite instance var testSuite = testSuiteOrWorkItem.DuckCast <ITestSuite>(); foreach (var item in testSuite.Tests) { Scope scope = NUnitIntegration.CreateScope(item.DuckCast <ITest>(), typeof(TTarget)); NUnitIntegration.FinishSkippedScope(scope, skipMessage); } } else if (typeName == "CompositeWorkItem") { // In case we have a CompositeWorkItem var compositeWorkItem = testSuiteOrWorkItem.DuckCast <ICompositeWorkItem>(); foreach (var item in compositeWorkItem.Children) { Scope scope = NUnitIntegration.CreateScope(item.DuckCast <IWorkItem>().Result.Test, typeof(TTarget)); NUnitIntegration.FinishSkippedScope(scope, skipMessage); } } } } return(default);