Output Formatters

Behat supports different ways of printing output information. Output printersin Behat are calledformats orformatters. You can tell Behat torun with a specific formatter by providing the--format option:

$behat--formatprogress

Note

The default formatter ispretty.

Behat supports 3 formatters out of the box:

  • pretty - prints the feature as is, with the full text of each step.

    ../../_images/formatter-pretty.png
  • progress - prints one character per step:

    ../../_images/formatter-progress.png
  • junit - prints the output to xml files in the standard junit.xml format

  • json - prints the output to a json file in json format.

    You can see the schema of this json file in theSchema definition in GitHub

If you don’t want to print output to the console, you can tell Behatto print output to a file instead ofSTDOUT with the--out option:

$behat--formatpretty--outreport.txt

Note

Some formatters, likejunit orjson, always require the--out option to bespecified. Thejunit formatter generates*.xml files for everysuite, so it needs a destination directory to put these XML files into. Thejson formatteroutputs a single file, so it needs the path of this file (which will be created if it doesnot exist)

Also, you can specify multiple formats to be used by Behat using multiple –format options:

$behat--formatpretty--formatprogress

In this case, default output will be used as output for both formatters. But if you wantthem to use different ones - specify them with--out:

$behat-fpretty-o~/pretty.out-fprogress-ostd-fjunit-oxml

In this case, output of pretty formatter will be written to~/pretty.out file, output of junitformatter will be written toxml folder and progress formatter will just print to console.

Behat tries hard to identify if your terminal supports colors or not, butsometimes it still fails. In such cases, you can force Behat touse colors (or not) with the options--colors or--no-colors,respectively:

$behat--no-colors

Format Options

The formatters can be configured with some options. The following options are available forall formatters:

  • output_verbosity indicates the level of detail of the output. Use one of theOutputFactory::* constants

  • output_path indicates the path where the output should be saved. Equivalent to the--out commandline option. Should be a file or folder, depending on the formatter.

  • output_decorate determines whether the output generated by Behat is “decorated” with formatting,such as colors, bold text, or other visual enhancements. Should be a boolean, defaults to true.

  • output_styles can be used to override the default styles used by Behat to display the different outputelements. It should be an array where the key is the style that needs to be overridden and which points to an array ofthree values. The first one is the foreground color, the second one the background color and the third one an array ofoptional styles.

The styles available for redefinition are:

  • keyword style of Gherkin keywords

  • stdout style of stdout output

  • exception style of exceptions

  • undefined style of undefined steps

  • pending style of pending steps

  • pending_param style of pending step params

  • failed style of failed steps

  • failed_param style of failed step params

  • passed style of passed steps

  • passed_param style of passed steo params

  • skipped style of skipped steps

  • skipped_param style of skipped step params

  • comment style of comments

  • tag style of scenario/feature tags

Available colors for first two arguments (fg andbg) are:black,red,green,yellow,blue,magenta,cyan andwhite.

Available optional styles are:bold,underscore,blink,reverse andconceal

Pretty formatter

The following options are specific to the Pretty formatter:

  • timer show time and memory usage at the end of the test run. Boolean, defaults to true.

  • expand print each example of a scenario outline separately. Boolean, defaults to false.

  • paths display the file path and line number for each scenario and the context file and method for each step.Boolean, defaults to true.

  • multiline print out PyStrings and TableNodes in full. Boolean, defaults to true.

  • showOutput show the test stdout output as part of the formatter output. Should be one of theShowOutputOption enum values, defaults toShowOutputOption::Yes.

  • shortSummary show just a list of failing scenarios at the end of the output. If false, a full summary(which also includes a list of failing steps) will be printed. Defaults to true

  • printSkippedSteps If the output should include any steps which are skipped by the runner. Useful when youdon’t want to see all the skipped steps after a failed step. Defaults to true

Progress formatter

The following options are specific to the Progress formatter:

  • timer show time and memory usage at the end of the test run. Boolean, defaults to true.

  • showOutput show the test stdout output as part of the formatter output. Should be one of theShowOutputOption enum values, defaults toShowOutputOption::InSummary.

  • shortSummary show just a list of failing scenarios at the end of the output. If false, a full summary(which also includes a list of failing steps) will be printed. Defaults to false

JUnit formatter

The following options are specific to the JUnit formatter:

  • timer show time spent in each scenario and feature. Boolean, defaults to true.

JSON formatter

The following options are specific to the JSON formatter:

  • timer show time spent in each scenario, feature and suite. Boolean, defaults to true.

Setting format options

Format options can be set using thewithFormatter() function of theProfile PHP config class. For example:

useBehat\Config\Config;useBehat\Config\Profile;useBehat\Config\Formatter\PrettyFormatter;$profile=(newProfile('default'))->withFormatter((newPrettyFormatter(paths:false))->withOutputStyles(['comment'=>['black','white',['underscore','bold']]]));return(newConfig())->withProfile($profile);
These options can also be set on the command line by using the

--format-setting option which accepts a json object with this configuration. For example:

$behat--format-settings='{\"paths\": false}'

Disabling a formatter

You can disable a formatter so that it won’t be available by using thedisableFormatter() function of theProfile PHP config class. For example:

useBehat\Config\Config;useBehat\Config\Profile;useBehat\Config\Formatter\PrettyFormatter;$profile=(newProfile('default'))->disableFormatter(PrettyFormatter::NAME);return(newConfig())->withProfile($profile);