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.
progress- prints one character per step:
junit- prints the output to xml files in the standard junit.xml formatjson- 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_verbosityindicates the level of detail of the output. Use one of theOutputFactory::*constantsoutput_pathindicates the path where the output should be saved. Equivalent to the--outcommandline option. Should be a file or folder, depending on the formatter.output_decoratedetermines 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_stylescan 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:
keywordstyle of Gherkin keywordsstdoutstyle of stdout outputexceptionstyle of exceptionsundefinedstyle of undefined stepspendingstyle of pending stepspending_paramstyle of pending step paramsfailedstyle of failed stepsfailed_paramstyle of failed step paramspassedstyle of passed stepspassed_paramstyle of passed steo paramsskippedstyle of skipped stepsskipped_paramstyle of skipped step paramscommentstyle of commentstagstyle 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:
timershow time and memory usage at the end of the test run. Boolean, defaults to true.expandprint each example of a scenario outline separately. Boolean, defaults to false.pathsdisplay the file path and line number for each scenario and the context file and method for each step.Boolean, defaults to true.multilineprint out PyStrings and TableNodes in full. Boolean, defaults to true.showOutputshow the test stdout output as part of the formatter output. Should be one of theShowOutputOptionenum values, defaults toShowOutputOption::Yes.shortSummaryshow 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 trueprintSkippedStepsIf 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:
timershow time and memory usage at the end of the test run. Boolean, defaults to true.showOutputshow the test stdout output as part of the formatter output. Should be one of theShowOutputOptionenum values, defaults toShowOutputOption::InSummary.shortSummaryshow 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:
timershow time spent in each scenario and feature. Boolean, defaults to true.
JSON formatter¶
The following options are specific to the JSON formatter:
timershow 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-settingoption 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);
Behat