/// <summary>
        /// OnMethodBegin callback
        /// </summary>
        /// <typeparam name="TTarget">Type of the target</typeparam>
        /// <typeparam name="TSuite">Test suite type</typeparam>
        /// <typeparam name="TResultState">Result state type</typeparam>
        /// <param name="instance">Instance value, aka `this` of the instrumented method.</param>
        /// <param name="testSuite">Test suite instance</param>
        /// <param name="resultState">Result state instance</param>
        /// <param name="message">Message instance</param>
        /// <returns>Calltarget state value</returns>
        internal static CallTargetState OnMethodBegin <TTarget, TSuite, TResultState>(TTarget instance, TSuite testSuite, TResultState resultState, string message)
        {
            if (testSuite is not null)
            {
                string typeName = testSuite.GetType().Name;

                if (typeName == "CompositeWorkItem")
                {
                    // In case we have a CompositeWorkItem we check if there is a OneTimeSetUp failure
                    var compositeWorkItem = testSuite.DuckCast <ICompositeWorkItem>();

                    if (compositeWorkItem.Result?.ResultState?.Status == TestStatus.Failed && compositeWorkItem.Result.ResultState.Site == FailureSite.SetUp)
                    {
                        foreach (var item in compositeWorkItem.Children)
                        {
                            if (item.GetType().Name == "CompositeWorkItem")
                            {
                                var compositeWorkItem2 = item.DuckCast <ICompositeWorkItem>();
                                foreach (var item2 in compositeWorkItem2.Children)
                                {
                                    var   testResult = item2.DuckCast <IWorkItem>().Result;
                                    Scope scope      = NUnitIntegration.CreateScope(testResult.Test, typeof(TTarget));
                                    scope.Span.Status = SpanStatus.Error;
                                    scope.Span.SetTag(Tags.ErrorMsg, compositeWorkItem.Result.Message);
                                    scope.Span.SetTag(Tags.ErrorStack, compositeWorkItem.Result.StackTrace);
                                    scope.Span.SetTag(Tags.ErrorType, "SetUpException");
                                    scope.Span.SetTag(TestTags.Status, TestTags.StatusFail);
                                    scope.Span.Finish(new TimeSpan(10));
                                    scope.Dispose();

                                    // we need to track all items that we tagged as error due this method uses recursion on child spans.
                                    _errorSpansFromCompositeWorkItems.GetOrCreateValue(item2);
                                }
                            }
                            else
                            {
                                var   testResult = item.DuckCast <IWorkItem>().Result;
                                Scope scope      = NUnitIntegration.CreateScope(testResult.Test, typeof(TTarget));
                                scope.Span.Status = SpanStatus.Error;
                                scope.Span.SetTag(Tags.ErrorMsg, compositeWorkItem.Result.Message);
                                scope.Span.SetTag(Tags.ErrorStack, compositeWorkItem.Result.StackTrace);
                                scope.Span.SetTag(Tags.ErrorType, "SetUpException");
                                scope.Span.SetTag(TestTags.Status, TestTags.StatusFail);
                                scope.Span.Finish(new TimeSpan(10));
                                scope.Dispose();

                                // we need to track all items that we tagged as error due this method uses recursion on child spans.
                                _errorSpansFromCompositeWorkItems.GetOrCreateValue(item);
                            }
                        }

                        return(new CallTargetState((Scope)null, (object)null));
                    }
                }
            }

            return(new CallTargetState(null, new object[] { testSuite, message }));
        }
        /// <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))));
        }
Exemple #3
0
        /// <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))));
        }
Exemple #4
0
        /// <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);