liveBook · Manning

Supplementary attachment pattern

As explained in Effective Behavior-Driven Development

A scenario-automation pattern that attaches relevant context to an assertion error message when it cannot be included in the message itself.

When an assertion fails, its message may not have room for all the useful context—or the context may not be text at all. The supplementary attachment pattern adds that context to the test result so a failure is easier to diagnose. section 19.7

For a scenario that fails:

  • Save useful context, such as an application log or screenshot. section 19.2
  • Register the saved file with the scenario automation tool or underlying test framework; saving it to the output directory alone usually does not attach it to the result. section 19.2

Application log

A failing UI scenario can attach the application log to help investigate unexpected popups or timing issues. A report can then show both the assertion message and the log attachment. section 19.2

Listing 19.4 Save and register supplementary attachment
*** Support/Hooks.cs ***
[Binding]
public class Hooks(
    IScenarioContext scenarioContext,    #A
    IReqnrollOutputHelper outputHelper,    #B
    AppLogContext appLogContext)    #C
{
  [AfterScenario(Order = 0)]    #D
  public void SaveAppLogOnError()
  {
    if (scenarioContext.ScenarioExecutionStatus == 
        ScenarioExecutionStatus.TestError)    #E
    {
      string outputPath = Path.GetFullPath(
        $"app-log-{Guid.NewGuid():N}.txt");    #F
      appLogContext.SaveToFile(outputPath);    #G
      outputHelper.AddAttachment(outputPath);    #H
    }
  }
}

See a Reqnroll hook that checks for a scenario error, saves the application log to a uniquely named file, and registers it as an attachment. section 19.2

How Effective Behavior-Driven Development covers it

How deep it goes
Defines the pattern and demonstrates a Reqnroll implementation, while leaving other frameworks to their documentation.
Examples
Uses an application log or UI screenshot as supplementary context for a failed scenario.
What you can do after
Diagnose scenario failures using attached application logs, screenshots, or other useful context.
How it connects
Connects with Descriptive Assertion, Red-Green-Refactor, Test Log, and Embedded Application Log patterns.

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.