/// <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); }
/// <summary> /// Initializes a new instance of the <see cref="AgentTesterServer{TStartup}"/> class enabling configuration of the <see cref="TestServer"/>. /// </summary> /// <param name="environmentVariablePrefix">The prefix that the environment variables must start with (will automatically add a trailing underscore where not supplied).</param> /// <param name="environment">The environment to be used by the underlying web host.</param> /// <param name="config">The <see cref="IConfiguration"/>; defaults to <see cref="AgentTester.BuildConfiguration{TStartup}(string?, string?)"/> where <c>null</c>.</param> /// <param name="services">An optional action to perform further <see cref="IServiceCollection"/> configuration.</param> /// <param name="configureLocalRefData">Indicates whether the pre-set local <see cref="TestSetUp.SetDefaultLocalReferenceData{TRefService, TRefProvider, TRefAgentService, TRefAgent}">reference data</see> is configured.</param> internal AgentTesterServer(string?environmentVariablePrefix = null, string environment = TestSetUp.DefaultEnvironment, IConfiguration?config = null, Action <IServiceCollection>?services = null, bool configureLocalRefData = true) : base(configureLocalRefData) { var action = new Action <IServiceCollection>(sc => { services?.Invoke(sc); sc.AddLogging(configure => configure.AddCorrelationId()); ReplaceEventPublisher(sc); }); var whb = new WebHostBuilder() .UseEnvironment(environment) .UseStartup <TStartup>() .ConfigureTestServices(action) .UseConfiguration(config ?? AgentTester.BuildConfiguration <TStartup>(environmentVariablePrefix, environment)); TestServer = new TestServer(whb); }
/// <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="ExecutionContext.Current"/> <see cref="ExecutionContext.Username"/>).</param> /// <param name="args">Optional argument that will be passed into the creation of the <see cref="ExecutionContext"/> (via the <see cref="AgentTester.CreateExecutionContext"/> function).</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) : base(AgentTester.UsernameConverter(userIdentifier) ?? AgentTester.DefaultUsername) { _username = AgentTester.UsernameConverter(userIdentifier) ?? AgentTester.DefaultUsername; _needsSetUp = needsSetUp; _args = args; }
/// <summary> /// Provides the opportunity to further configure the <i>local</i> (non-API) test <see cref="IServiceCollection"/>. /// </summary> /// <param name="serviceCollection">The <see cref="IServiceCollection"/> action.</param> protected void ConfigureLocalServices(Action <IServiceCollection> serviceCollection) => AgentTester.ConfigureLocalServices(serviceCollection);
/// <summary> /// Initializes a new instance of the <see cref="AgentTesterRunArgs{TAgent}"/> class. /// </summary> /// <param name="client">The <see cref="HttpClient"/>.</param> /// <param name="beforeRequest">The action to run before the request is made.</param> /// <param name="tester">The executing <see cref="AgentTester"/>.</param> internal AgentTesterRunArgs(HttpClient client, Action <HttpRequestMessage> beforeRequest, AgentTester <TAgent> tester) : base(client, beforeRequest) => Tester = tester;