Output API

The SpecFlow Output API allows you to display texts and attachments in your IDE’s test explorer output window and also in SpecFlow+LivingDoc.

To use the SpecFlow output API interface you must inject the ISpecFlowOutputHelper interface via Context Injection:

    private readonly ISpecFlowOutputHelper _specFlowOutputHelper;

    public CalculatorStepDefinitions(ISpecFlowOutputHelper outputHelper)
        _outputHelper = outputHelper;

There are two methods available:

WriteLine(string text)

This method adds text:


AddAttachment(string filePath)

This method adds an attachment and requires the file path:


> Note: The attachment file can be stored anywhere. But it is important to keep mind that if a local file is added, it will only work on your machine and not accessible when shared with others.

> Note: Handling of attachments depends on your runner. MStest and NUnit currently support this feature but xUnit and SpecFlow+ Runner do not.


This example is based on the Selenium with Page Object Model Pattern project which tests a simple calculator web application. You can download the repo for this example here.

The _specFlowOutputHelper.WriteLine is used to indicate when the browser launches and closes:

Writeline example

Since this project tests a web application using a browser, the _specFlowOutputHelper.AddAttachment method has been used in the logging Hooks to display the saved screen shots taken during testing:

Writeattachment example


To view the output window in Visual Studio, go to the text explorer and click on Open Additional output for this result:

Test Explorer in VS

The resulting output for the example project used above would look like this:

Output in VS

The added text lines on browser launch and termination can help you easily identify the exact point at which the action takes place. The screen shots taken during testing are all also listed as image files under Attachments.

If an attachment fails, the output explorer will display this message:

Output error

In SpecFlow+LivingDoc, no additional setup is required, simply generate LivingDoc as you normally do. You can then view the output texts and attachments by toggling the Show/Hide Test Output :

Output API in LivingDoc

> Note: If the test output toggle is missing, it may be that you are on an older version of SpecFlow+LivingDoc, click here to update to the latest version.

> Note: The Output API in SpecFlow+LivingDoc supports the following four Hooks :

  • BeforeScenario,

  • AfterScenario,

  • BeforeStep,

  • AfterStep