Пример #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);
            }
Пример #5
0
 /// <summary>
 /// Create a new <see cref="GrpcAgentTest{TStartup, TAgent, TValue}"/> for a named <paramref name="userIdentifier"/> (converted using <see cref="TestSetUp.ConvertUsername(object?)"/>).
 /// </summary>
 /// <typeparam name="TAgent">The <b>Agent</b> <see cref="Type"/>.</typeparam>
 /// <typeparam name="TValue">The response value <see cref="Type"/>.</typeparam>
 /// <param name="userIdentifier">The user identifier (<c>null</c> indicates to use the <see cref="ExecutionContext.Current"/> <see cref="ExecutionContext.Username"/>).</param>
 /// <param name="args">Optional argument that can be referenced within the test.</param>
 /// <returns>An <see cref="GrpcAgentTest{TStartup, TAgent, TValue}"/> instance</returns>
 public GrpcAgentTest <TStartup, TAgent, TValue> TestGrpc <TAgent, TValue>(object userIdentifier, object?args = null) where TAgent : GrpcAgentBase
 {
     return(new GrpcAgentTest <TStartup, TAgent, TValue>(this, TestSetUp.ConvertUsername(userIdentifier), args));
 }
Пример #6
0
 /// <summary>
 /// Create a new <see cref="AgentTest{TStartup, TAgent}"/> for a named <paramref name="userIdentifier"/> (converted using <see cref="TestSetUp.ConvertUsername(object?)"/>).
 /// </summary>
 /// <typeparam name="TAgent">The <b>Agent</b> <see cref="Type"/>.</typeparam>
 /// <param name="userIdentifier">The user identifier (<c>null</c> indicates to use the <see cref="ExecutionContext.Current"/> <see cref="ExecutionContext.Username"/>).</param>
 /// <param name="args">Optional argument that can be referenced within the test.</param>
 /// <returns>An <see cref="AgentTest{TStartup, TAgent}"/> instance.</returns>
 public AgentTest <TStartup, TAgent> Test <TAgent>(object?userIdentifier, object?args = null) where TAgent : WebApiAgentBase
 {
     return(new AgentTest <TStartup, TAgent>(this, TestSetUp.ConvertUsername(userIdentifier), args));
 }
Пример #7
0
 /// <summary>
 /// Wraps a command and returns the result.
 /// </summary>
 /// <param name="command">The <see cref="TestCommand"/> to be wrapped.</param>
 /// <returns>The wrapped <see cref="TestCommand"/>.</returns>
 public TestCommand Wrap(TestCommand command)
 {
     TestSetUp.ShouldContinueRunningTestsAssert();
     return(new ExecutionContextCommand(command, _needsSetup));
 }
Пример #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TestSetUpAttribute"/> class for a <paramref name="userIdentifier"/>.
 /// </summary>
 /// <param name="userIdentifier">The user identifier (<c>null</c> indicates to use the <see cref="TestSetUp.DefaultUsername"/>).</param>
 /// <param name="args">Optional argument that will be passed into the creation of the <see cref="ExecutionContext"/>.</param>
 /// <param name="needsSetUp">Indicates whether the registered set up is required to be invoked for the test.</param>
 public TestSetUpAttribute(object?userIdentifier, object?args = null, bool needsSetUp = true) : this(TestSetUp.ConvertUsername(userIdentifier), args, needsSetUp)
 {
 }
Пример #9
0
 /// <summary>
 /// Create a new <see cref="GenericTester"/> for a named <paramref name="userIdentifier"/> (converted using <see cref="TestSetUp.ConvertUsername(object?)"/>).
 /// </summary>
 /// <param name="userIdentifier">The user identifier (<c>null</c> indicates to use the <see cref="ExecutionContext.Current"/> <see cref="ExecutionContext.Username"/>).</param>
 /// <param name="args">Optional argument that can be referenced within the test.</param>
 /// <param name="includeLoggingScopesInOutput">Indicates whether to include scopes in log output.</param>
 /// <returns>A <see cref="GenericTester"/> instance.</returns>
 public static GenericTester Test(object?userIdentifier, object?args = null, bool?includeLoggingScopesInOutput = null) => new GenericTester(TestSetUp.ConvertUsername(userIdentifier), args, includeLoggingScopesInOutput);
Пример #10
0
 public void UsingOneTimeSetUp()
 {
     TestSetUp.Reset(true, null);
     _agentTester = new AgentTesterServer <TStartup>();
 }
Пример #11
0
 public void UsingOneTimeSetUp()
 {
     TestSetUp.Reset(true, null);
     _agentTester = new AgentTesterServer <TStartup>(_environmentVariablePrefix, _environment, _config, _services, _configureLocalRefData, _includeLoggingScopesInOutput);
 }
Пример #12
0
 /// <summary>
 /// Create a new <see cref="ValidationTester"/> for a named <paramref name="userIdentifier"/> (converted using <see cref="TestSetUp.ConvertUsername(object?)"/>).
 /// </summary>
 /// <param name="userIdentifier">The user identifier (<c>null</c> indicates to use the <see cref="ExecutionContext.Current"/> <see cref="ExecutionContext.Username"/>).</param>
 /// <param name="args">Optional argument that can be referenced within the test.</param>
 /// <returns>A <see cref="ValidationTester"/> instance.</returns>
 public static ValidationTester Test(object?userIdentifier, object?args = null) => new ValidationTester(TestSetUp.ConvertUsername(userIdentifier), args);
Пример #13
0
 /// <summary>
 /// Create a new <see cref="GenericTester"/> for a named <paramref name="userIdentifier"/> (converted using <see cref="TestSetUp.ConvertUsername(object?)"/>).
 /// </summary>
 /// <param name="userIdentifier">The user identifier (<c>null</c> indicates to use the <see cref="ExecutionContext.Current"/> <see cref="ExecutionContext.Username"/>).</param>
 /// <param name="args">Optional argument that can be referenced within the test.</param>
 /// <returns>A <see cref="GenericTester"/> instance.</returns>
 public static GenericTester Test(object?userIdentifier, object?args = null) => new GenericTester(TestSetUp.ConvertUsername(userIdentifier), args);