Search code examples
phpbehat

Override / Extend behat ProgressStepPrinter


I am trying to create an extension to behat progress output step formatter.

The goal is simply change the . to a * for each passed scenario step.

The behat documentation does not even mention the ability to create a custom formatter, however I found a GitHub repository that does this behat-format-progress-fail

But no clear / simple overview of how to achieve this is there, I have tried duplicating and modifying them. Is there a simple example or clear documentation for this?


Solution

  • By creating a very cut down version of ProgressStepPrinter.php and then including that in autoload-dev in composer.json uses it in prefernce to the one from Behat.

    ProgressStepPrinter.php

    namespace Behat\Behat\Output\Node\Printer\Progress;
    
    use Behat\Behat\Output\Node\Printer\Helper\ResultToStringConverter;
    use Behat\Behat\Output\Node\Printer\StepPrinter;
    use Behat\Behat\Tester\Result\StepResult;
    use Behat\Gherkin\Node\ScenarioLikeInterface as Scenario;
    use Behat\Gherkin\Node\StepNode;
    use Behat\Testwork\Output\Formatter;
    
    final class ProgressStepPrinter implements StepPrinter {
    
        private $resultConverter;
    
        public function __construct(ResultToStringConverter $resultConverter) {
            $this->resultConverter = $resultConverter;
        }
    
        public function printStep(Formatter $formatter, Scenario $scenario, StepNode $step, StepResult $result) {
            //custom logic to print per step here
        }
    }
    

    composer.json

    "autoload-dev": {
        "files": [
            "features/bootstrap/ProgressStepPrinter.php",
        ]
    },