From 31dd209b43bc5b06185ba017772840d54197c279 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 25 Nov 2025 17:13:39 +1100 Subject: [PATCH] [#45] Fixed metrics doubling when formatter used with other formatters. --- .../FormatExtension.php | 13 +++-- tests/behat/features/format.feature | 56 +++++++++++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/src/DrevOps/BehatFormatProgressFail/FormatExtension.php b/src/DrevOps/BehatFormatProgressFail/FormatExtension.php index cfc8f06..5a33aa0 100644 --- a/src/DrevOps/BehatFormatProgressFail/FormatExtension.php +++ b/src/DrevOps/BehatFormatProgressFail/FormatExtension.php @@ -13,6 +13,7 @@ use Behat\Behat\Output\Node\EventListener\Statistics\ScenarioStatsListener; use Behat\Behat\Output\Node\EventListener\Statistics\StepStatsListener; use Behat\Behat\Output\Node\EventListener\Statistics\HookStatsListener; +use Behat\Behat\Output\Statistics\TotalStatistics; use Behat\Testwork\Output\Printer\StreamOutputPrinter; use Behat\Behat\Output\Printer\ConsoleOutputFactory; use Behat\Testwork\Exception\ServiceContainer\ExceptionExtension; @@ -82,6 +83,10 @@ public function configure(ArrayNodeDefinition $builder): void { public function load(ContainerBuilder $container, array $config): void { $name = isset($config['name']) && is_string($config['name']) ? $config['name'] : self::MOD_ID; + // Create our own statistics object instead of using the shared one. + $definition = new Definition(TotalStatistics::class); + $container->setDefinition('output.progress_fail.statistics', $definition); + $definition = new Definition(StepListener::class, [ new Reference('output.printer.' . $name), ]); @@ -105,18 +110,18 @@ public function load(ContainerBuilder $container, array $config): void { [ new Reference(self::ROOT_LISTENER_ID), new Definition(StatisticsListener::class, [ - new Reference('output.progress.statistics'), + new Reference('output.progress_fail.statistics'), new Reference('output.node.printer.progress.statistics'), ]), new Definition(ScenarioStatsListener::class, [ - new Reference('output.progress.statistics'), + new Reference('output.progress_fail.statistics'), ]), new Definition(StepStatsListener::class, [ - new Reference('output.progress.statistics'), + new Reference('output.progress_fail.statistics'), new Reference(ExceptionExtension::PRESENTER_ID), ]), new Definition(HookStatsListener::class, [ - new Reference('output.progress.statistics'), + new Reference('output.progress_fail.statistics'), new Reference(ExceptionExtension::PRESENTER_ID), ]), ], diff --git a/tests/behat/features/format.feature b/tests/behat/features/format.feature index 7da5c3b..01a970d 100644 --- a/tests/behat/features/format.feature +++ b/tests/behat/features/format.feature @@ -332,3 +332,59 @@ Feature: Format 3 steps (2 passed, 1 failed) """ + Scenario: Metrics should not double when used with multiple formatters + Given a file named "behat.yml" with: + """ + default: + suites: + default: + contexts: + - FeatureContextTest + extensions: + DrevOps\BehatFormatProgressFail\FormatExtension: ~ + """ + And a file named "features/apples.feature" with: + """ + Feature: Apples story + In order to eat apple + As a little kid + I need to have an apple in my pocket + + Background: + Given I have 3 apples + + Scenario: I'm little hungry + When I ate 1 apple + Then I should have 2 apples + And I should have 2 apples + + Scenario: I eat wrong amount + When I ate 1 apple + Then I should have 3 apples + And I should have 2 apples + + Scenario: I found apples + When I found 2 apples + Then I should have 5 apples + And I should have 5 apples + """ + When I run "behat --no-colors --strict -f progress -o progress.txt -f progress_fail -o std" + Then it should fail with: + """ + ...... + --- FAIL --- + Then I should have 3 apples # (features/apples.feature):16 + Failed asserting that 2 matches expected 3. + ------------ + -.... + + --- Failed steps: + + 001 Scenario: I eat wrong amount # features/apples.feature:14 + Then I should have 3 apples # features/apples.feature:16 + Failed asserting that 2 matches expected 3. + + 3 scenarios (2 passed, 1 failed) + 12 steps (10 passed, 1 failed, 1 skipped) + """ +