allure-framework / allure-phpunit

Allure integrations for PHP test frameworks: PHPUnit
https://allurereport.org/
Apache License 2.0
60 stars 28 forks source link

Ability to write report only on explicit demand #111

Open antonkomarev opened 4 days ago

antonkomarev commented 4 days ago

Is your feature request related to a problem? Please describe. I want to avoid writing allure reports all the time on the developer local machine.

Describe the solution you'd like I want to have some kind of environment or argument for the PHP CLI command to enable report writing.

Some kind like

./vendor/bin/phpunit --with-allure-report

Describe alternatives you've considered I understand that developers might have local phpunit.xml, but it's not an easy to manage multiple configurations.

antonkomarev commented 3 days ago

Found a workaround using lifecycle hooks:

return [
    'outputDirectory' => './tests/allure-results',
    'lifecycleHooks' => [
        new class implements BeforeTestStartHookInterface, BeforeContainerStartHookInterface {
            public function beforeContainerStart(
                ContainerResult $container,
            ): void {
                if (!$this->isReportEnabled()) {
                    $container->setExcluded(true);
                }
            }

            public function beforeTestStart(
                TestResult $test,
            ): void {
                if (!$this->isReportEnabled()) {
                    $test->setExcluded(true);

                    return;
                }
            }

            private function isReportEnabled(): bool
            {
                return boolval(getenv('ALLURE_REPORT_ENABLED'));
            }
        },
    ],
];