liveBook · Manning

Layered automation code architecture

As explained in Effective Behavior-Driven Development

An architecture that separates the testing concern from the automation concern into distinct layers.

BDD automation has two concerns: turning specification statements into testable actions, and exercising the application to perform them. Separating them creates a layered architecture. section 15.2

Keep step definitions as the interface between the specification and automation, where testing decisions are made. Move application interactions into a driver instead of having one step definition call another. section 15.2

Authentication API driver

A driver can send a login request to the application API. Step definitions call it for valid or incorrect passwords and check the resulting behavior. section 15.2

Listing 15.6 Step definition class update to use the driver
*** StepDefinitions/AuthenticationStepDefinitions.cs ***
public class AuthenticationStepDefinitions(
    AuthenticationApiDriver authApiDriver)
{
  private HttpResponseMessage? loginApiResponse;

  [Given("the customer is authenticated")]
  public async Task GivenTheCustomerIsAuthenticated()
  {
    var response = await authApiDriver.PerformLogin(    #A
        DomainDefaults.CustomerName, DomainDefaults.Password);
    Assert.AreEqual(HttpStatusCode.OK,    #B
        response.StatusCode);
  }

  [When("the customer attempts to log in with a wrong password")]
  public async Task WhenTheCustomerAttemptsToLogInWithAWrongPassword()
  {
    loginApiResponse = await authApiDriver    #A
      .PerformLogin(DomainDefaults.CustomerName, 
          DomainDefaults.WrongPassword);
  }

  [Then("the login should fail with {string}")]
  public async Task ThenTheLoginShouldFailWith(string expectedMessage)
  {
    Assert.IsNotNull(loginApiResponse);
    Assert.AreNotEqual(HttpStatusCode.OK,    #B
        loginApiResponse.StatusCode);
    var errorMessage = await loginApiResponse.Content.ReadAsStringAsync();
    StringAssert.Contains(errorMessage, expectedMessage,
        "Login should fail with the right error message");
  }
}

See step definitions delegate login requests to an authentication API driver. section 15.2

How Effective Behavior-Driven Development covers it

How deep it goes
Distinguishes two concerns and demonstrates moving login API interaction into a driver.
Examples
Uses customer login scenarios with valid and incorrect passwords and an authentication API driver.
What you can do after
Apply the pattern by delegating application interactions from step definitions to a driver.
How it connects
Connects BDD scenarios, step definitions, test actions, and the system under test.

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.