diff --git a/src/Codeception/Codecept.php b/src/Codeception/Codecept.php index 44eb61484d..6d2c0a518a 100644 --- a/src/Codeception/Codecept.php +++ b/src/Codeception/Codecept.php @@ -133,7 +133,7 @@ public function registerSubscribers() $this->dispatcher->addSubscriber(new Subscriber\Console($this->options)); } if ($this->options['fail-fast']) { - $this->dispatcher->addSubscriber(new Subscriber\FailFast()); + $this->dispatcher->addSubscriber(new Subscriber\FailFast($this->options['fail-fast'])); } if ($this->options['coverage']) { diff --git a/src/Codeception/Command/Run.php b/src/Codeception/Command/Run.php index 8f6243f83c..3fc3ef65b9 100644 --- a/src/Codeception/Command/Run.php +++ b/src/Codeception/Command/Run.php @@ -81,7 +81,7 @@ * --skip (-s) Skip selected suites (multiple values allowed) * --skip-group (-x) Skip selected groups (multiple values allowed) * --env Run tests in selected environments. (multiple values allowed, environments can be merged with ',') - * --fail-fast (-f) Stop after first failure + * --fail-fast (-f) Stop after nth failure (defaults to 1) * --no-rebuild Do not rebuild actor classes on start * --help (-h) Display this help message. * --quiet (-q) Do not output any message. Almost the same as `--silent` @@ -213,7 +213,7 @@ protected function configure() InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Run tests in selected environments.' ), - new InputOption('fail-fast', 'f', InputOption::VALUE_NONE, 'Stop after first failure'), + new InputOption('fail-fast', 'f', InputOption::VALUE_OPTIONAL, 'Stop after nth failure'), new InputOption('no-rebuild', '', InputOption::VALUE_NONE, 'Do not rebuild actor classes on start'), new InputOption( 'seed', @@ -325,6 +325,10 @@ public function execute(InputInterface $input, OutputInterface $output) if (!$userOptions['ansi'] && $input->getOption('colors')) { $userOptions['colors'] = true; // turn on colors even in non-ansi mode if strictly passed } + // array key will exist if fail-fast option is used + if (array_key_exists('fail-fast', $userOptions)) { + $userOptions['fail-fast'] = (int) $this->options['fail-fast'] ?: 1; + } $suite = $input->getArgument('suite'); $test = $input->getArgument('test'); diff --git a/src/Codeception/Subscriber/FailFast.php b/src/Codeception/Subscriber/FailFast.php index 2f40529d3d..37e8d21eea 100644 --- a/src/Codeception/Subscriber/FailFast.php +++ b/src/Codeception/Subscriber/FailFast.php @@ -2,6 +2,7 @@ namespace Codeception\Subscriber; use Codeception\Event\SuiteEvent; +use Codeception\Event\TestEvent; use Codeception\Events; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -10,12 +11,33 @@ class FailFast implements EventSubscriberInterface use Shared\StaticEvents; public static $events = [ - Events::SUITE_BEFORE => 'stopOnFail', + Events::TEST_FAIL => 'stopOnFail', + Events::TEST_ERROR => 'stopOnFail', + Events::SUITE_BEFORE => 'cacheSuite' ]; - public function stopOnFail(SuiteEvent $e) + private $failureCount = 0; + + private $stopFailureCount; + + private $suiteCache; + + public function __construct($stopFailureCount) + { + $this->stopFailureCount = (int) $stopFailureCount; + } + + public function cacheSuite(SuiteEvent $e) { - $e->getResult()->stopOnError(true); - $e->getResult()->stopOnFailure(true); + $this->suiteCache = $e->getResult(); + } + + public function stopOnFail(TestEvent $e) + { + $this->failureCount++; + + if ($this->failureCount >= $this->stopFailureCount) { + $this->suiteCache->stop(); + } } } diff --git a/tests/cli/RunCest.php b/tests/cli/RunCest.php index 162e038faa..222ac090d9 100755 --- a/tests/cli/RunCest.php +++ b/tests/cli/RunCest.php @@ -296,7 +296,7 @@ public function runOneGroupWithDataProviders(\CliGuy $I) $I->seeInShellOutput("OK"); } - public function runTestWithFailFast(\CliGuy $I) + public function runTestWithFailFastDefault(\CliGuy $I) { $I->executeCommand('run unit --skip-group error --no-exit'); $I->seeInShellOutput('FailingTest: Me'); @@ -306,6 +306,14 @@ public function runTestWithFailFast(\CliGuy $I) $I->dontSeeInShellOutput("PassingTest: Me"); } + public function runTestWithFailFastCustom(\CliGuy $I) + { + $I->executeCommand('run unit tests/unit/FailingTest.php --fail-fast=2 --no-exit'); + $I->seeInShellOutput('There were 2 failures'); + $I->executeCommand('run unit tests/unit/FailingTest.php --no-exit'); + $I->seeInShellOutput('There were 3 failures'); + } + public function runWithCustomOutputPath(\CliGuy $I) { $I->executeCommand('run dummy --xml myverycustom.xml --html myownhtmlreport.html'); diff --git a/tests/data/claypit/tests/unit/FailingTest.php b/tests/data/claypit/tests/unit/FailingTest.php index 3229f9231b..48bee42991 100644 --- a/tests/data/claypit/tests/unit/FailingTest.php +++ b/tests/data/claypit/tests/unit/FailingTest.php @@ -7,4 +7,13 @@ public function testMe() $this->assertFalse(true); } + public function testMeTwo() + { + $this->assertFalse(true); + } + + public function testMeThree() + { + $this->assertFalse(true); + } } \ No newline at end of file