liveBook · Manning

Test action pattern

As explained in Effective Behavior-Driven Development

A pattern in which test-action infrastructure interacts with an application and captures functional failures and health incidents.

Test actions give automation code a uniform way to perform an action and apply shared functionality, such as logging, timing, and failure handling. This keeps step definitions focused on testing decisions and makes actions easier to reuse and reorganize. section 15.0 section 15.4

A practical implementation separates the shared execution behavior from the action itself:

  • A base class wraps execution with logging, elapsed-time reporting, and exception logging before rethrowing failures. section 15.4
  • Concrete actions provide the work to perform. For simple actions, a lambda-based action avoids creating a separate derived class; more complicated actions can use dedicated classes. section 15.4
Listing 15.13 Base class for test actions
*** Support/TestAction.cs ***
public abstract class TestAction<TResult>(string actionName)
{
  public string TestActionName => testActionName;

  protected abstract Task<TResult> DoExecute();    #A

  public async Task<TResult> Execute()
  {
    try
    {
      Console.WriteLine($"Executing {TestActionName}...");
      var stopwatch = Stopwatch.StartNew();
      var result = await DoExecute();    #B
      Console.WriteLine(
        $"{TestActionName} executed successfully in {stopwatch.Elapsed}.");
      return result;
    }
    catch (Exception ex)
    {
      Console.WriteLine($"{TestActionName} failed: {ex.Message}");
      throw;
    }
  }
}

See how a base action wraps the delegated work with logging, timing, and exception handling. section 15.4

How Effective Behavior-Driven Development covers it

How deep it goes
Implements a shared execution wrapper and compares derived actions with lambdas, while leaving design choices language-dependent.
Examples
Uses authentication API actions, including login and registration, to demonstrate dispatch and implementation.
What it assumes
Assumes an automation codebase with step definitions and API drivers, while introducing a shared action-dispatch structure.
What you can do after
Build reusable test actions with shared logging, timing, and failure handling.
How it connects
Connects test actions with step definitions, API drivers, and cross-cutting automation concerns.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Effective Behavior-Driven Development. The section links point to the parts of the book it draws on.