Skip to content

Before and After actions

WARNING

The Before/After actions are not additive. The last one specified is the only one executed.

Alba allows you to specify actions that run immediately before or after an HTTP request is executed for common setup or teardown work like setting up authentication credentials or tracing or whatever.

Here's a sample:

cs
// Synchronously
system.BeforeEach(context =>
{
    // Modify the HttpContext immediately before each
    // Scenario()/HTTP request is executed
    context.Request.Headers.Append("trace", "something");
});

system.AfterEach(context =>
{
    // perform an action immediately after the scenario/HTTP request
    // is executed
});

// Asynchronously
system.BeforeEachAsync(context =>
{
    // do something asynchronous here
    return Task.CompletedTask;
});

system.AfterEachAsync(context =>
{
    // do something asynchronous here
    return Task.CompletedTask;
});

snippet source | anchor