liveBook · Manning

Test action

As explained in Effective Behavior-Driven Development

A unit of automation logic that performs an operation against the system under test as part of a behavioral test.

A test action is an executable step that prepares, exercises, or verifies an aspect of an application. It gives a driver a way to wrap an operation in shared concerns such as logging and timing, rather than repeating that logic in every driver method. section 15.4 section 15.9

A driver can create several actions, such as login and registration. Step definitions execute them; more complex tests can retain an action to check execution errors or expected results. Actions can use a shared base class, or a lambda-based implementation when a separate derived class is unnecessary. 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 execution with logging, timing, and exception reporting while delegating the operation to DoExecute. section 15.4

How Effective Behavior-Driven Development covers it

How deep it goes
Defines a base execution wrapper and shows derived or lambda-based implementations for performing actions.
Examples
Uses login and registration actions that call a REST API and can return a login response.
What you can do after
Apply the pattern to execute driver operations with shared logging, timing, and exception reporting.
How it connects
Connects test drivers and step definitions, and relates outcome verification to the Attempt Action pattern.

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.