Пример #1
0
            /// <summary>
            /// Executes the test, saving a <see cref="TestResult"/> in the supplied <see cref="TestExecutionContext"/>.
            /// </summary>
            /// <param name="context">The <see cref="TestExecutionContext"/>.</param>
            /// <returns>The <see cref="TestResult"/>.</returns>
            public override TestResult Execute(TestExecutionContext context)
            {
                try
                {
                    if (_needsSetUp)
                    {
                        TestSetUp.InvokeRegisteredSetUp();
                    }

                    _username.Value = _testUsername;
                    _args.Value     = _testArgs;

                    if (context.CurrentTest?.Parent?.Fixture is ITestSetupPrepareExecutionContext uats)
                    {
                        uats.AgentTester.PrepareExecutionContext();
                    }
                    else
                    {
                        ExecutionContext.Reset();
                    }

                    context.CurrentResult = innerCommand.Execute(context);
                }
                catch (Exception exception)
                {
                    Exception ex = exception;
                    if (ex is AggregateException aex && aex.InnerExceptions.Count == 1)
                    {
                        ex = aex.InnerException !;
                    }

                    if (ex is NUnitException nex && nex.InnerException is AggregateException aex2)
                    {
                        ex = aex2.InnerException !;
                    }

                    if (context.CurrentResult == null)
                    {
                        context.CurrentResult = context.CurrentTest.MakeTestResult();
                    }

                    context.CurrentResult.RecordException(ex);
                }
                finally
                {
                    ExecutionContext.Reset();
                }

                // Remove any extraneous assertion results as this clutters/confuses the output.
                for (int i = context.CurrentResult.AssertionResults.Count - 1; i > 0; i--)
                {
                    context.CurrentResult.AssertionResults.RemoveAt(i);
                }

                _username.Value = TestSetUp.DefaultUsername;
                _args.Value     = null;
                return(context.CurrentResult);
            }
Пример #2
0
            /// <summary>
            /// Executes the test, saving a <see cref="TestResult"/> in the supplied <see cref="TestExecutionContext"/>.
            /// </summary>
            /// <param name="context">The <see cref="TestExecutionContext"/>.</param>
            /// <returns>The <see cref="TestResult"/>.</returns>
            public override TestResult Execute(TestExecutionContext context)
            {
                try
                {
                    if (_needsSetUp)
                    {
                        TestSetUp.InvokeRegisteredSetUp();
                    }

                    if (context.CurrentTest?.Parent?.Fixture is ITestSetupPrepareExecutionContext uats)
                    {
                        uats.AgentTester.PrepareExecutionContext();
                    }
                    else
                    {
                        ExecutionContext.Reset();
                    }

                    context.CurrentResult = innerCommand.Execute(context);
                }
#pragma warning disable CA1031 // Do not catch general exception types; by-design, need to catch them all and bubble out so that NUnit reports correctly.
                catch (Exception exception)
                {
                    Exception ex = exception;
                    if (ex is AggregateException aex && aex.InnerExceptions.Count == 1)
                    {
                        ex = aex.InnerException !;
                    }

                    if (ex is NUnitException nex && nex.InnerException is AggregateException aex2)
                    {
                        ex = aex2.InnerException !;
                    }

                    if (context.CurrentResult == null)
                    {
                        context.CurrentResult = context.CurrentTest.MakeTestResult();
                    }

                    context.CurrentResult.RecordException(ex);
                }
#pragma warning restore CA1031
                finally
                {
                    ExecutionContext.Reset();
                }

                // Remove any extraneous assertion results as this clutters/confuses the output.
                for (int i = context.CurrentResult.AssertionResults.Count - 1; i > 0; i--)
                {
                    context.CurrentResult.AssertionResults.RemoveAt(i);
                }

                _username.Value = TestSetUp.DefaultUsername;
                _args.Value     = null;
                return(context.CurrentResult);
            }
Пример #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AgentTester"/> class using the specified details.
        /// </summary>
        /// <param name="username">The username (<c>null</c> indicates to use the <see cref="ExecutionContext.Current"/> <see cref="ExecutionContext.Username"/>; otherwise, create using <see cref="CreateExecutionContext"/>).</param>
        /// <param name="args">Optional argument that can be referenced within the test.</param>
        protected AgentTester(string username = null, object args = null)
        {
            TestSetUp.InvokeRegisteredSetUp();

            if (username != null || !ExecutionContext.HasCurrent)
            {
                ExecutionContext.Reset(false);
                ExecutionContext.SetCurrent(CreateExecutionContext.Invoke(username ?? DefaultUsername, args));
            }

            Args     = args;
            Username = ExecutionContext.Current.Username;
        }
Пример #4
0
            /// <summary>
            /// Executes the test, saving a <see cref="TestResult"/> in the supplied <see cref="TestExecutionContext"/>.
            /// </summary>
            /// <param name="context">The <see cref="TestExecutionContext"/>.</param>
            /// <returns>The <see cref="TestResult"/>.</returns>
            public override TestResult Execute(TestExecutionContext context)
            {
                try
                {
                    if (_needsSetUp)
                    {
                        TestSetUp.InvokeRegisteredSetUp();
                    }

                    ExecutionContext.Reset(false);
                    ExecutionContext.SetCurrent(AgentTester.CreateExecutionContext(_username, _args));
                    ExecutionContext.Current.Properties["InvokeRegisteredSetUp"] = _needsSetUp;

                    context.CurrentResult = this.innerCommand.Execute(context);
                }
#pragma warning disable CA1031 // Do not catch general exception types; by-design, need to catch them all.
                catch (Exception exception)
                {
                    Exception ex = exception;
                    if (ex is AggregateException aex && aex.InnerExceptions.Count == 1)
                    {
                        ex = aex.InnerException !;
                    }

                    if (ex is NUnitException nex && nex.InnerException is AggregateException aex2)
                    {
                        ex = aex2.InnerException !;
                    }

                    if (context.CurrentResult == null)
                    {
                        context.CurrentResult = context.CurrentTest.MakeTestResult();
                    }

                    context.CurrentResult.RecordException(ex);
                }
#pragma warning restore CA1031
                finally
                {
                    ExecutionContext.Reset(false);
                    Factory.ResetLocal();
                }

                // Remove any extraneous assertion results as this clutters/confuses the output.
                for (int i = context.CurrentResult.AssertionResults.Count - 1; i > 0; i--)
                {
                    context.CurrentResult.AssertionResults.RemoveAt(i);
                }

                return(context.CurrentResult);
            }