Skip to content

Commit 29cd2d1

Browse files
committed
Added optional value to fail-fast option
1 parent f8881df commit 29cd2d1

File tree

5 files changed

+51
-8
lines changed

5 files changed

+51
-8
lines changed

src/Codeception/Codecept.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function registerSubscribers()
133133
$this->dispatcher->addSubscriber(new Subscriber\Console($this->options));
134134
}
135135
if ($this->options['fail-fast']) {
136-
$this->dispatcher->addSubscriber(new Subscriber\FailFast());
136+
$this->dispatcher->addSubscriber(new Subscriber\FailFast($this->options['fail-fast']));
137137
}
138138

139139
if ($this->options['coverage']) {

src/Codeception/Command/Run.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
* --skip (-s) Skip selected suites (multiple values allowed)
8282
* --skip-group (-x) Skip selected groups (multiple values allowed)
8383
* --env Run tests in selected environments. (multiple values allowed, environments can be merged with ',')
84-
* --fail-fast (-f) Stop after first failure
84+
* --fail-fast (-f) Stop after nth failure (defaults to 1)
8585
* --no-rebuild Do not rebuild actor classes on start
8686
* --help (-h) Display this help message.
8787
* --quiet (-q) Do not output any message. Almost the same as `--silent`
@@ -213,7 +213,7 @@ protected function configure()
213213
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
214214
'Run tests in selected environments.'
215215
),
216-
new InputOption('fail-fast', 'f', InputOption::VALUE_NONE, 'Stop after first failure'),
216+
new InputOption('fail-fast', 'f', InputOption::VALUE_OPTIONAL, 'Stop after nth failure'),
217217
new InputOption('no-rebuild', '', InputOption::VALUE_NONE, 'Do not rebuild actor classes on start'),
218218
new InputOption(
219219
'seed',
@@ -325,6 +325,10 @@ public function execute(InputInterface $input, OutputInterface $output)
325325
if (!$userOptions['ansi'] && $input->getOption('colors')) {
326326
$userOptions['colors'] = true; // turn on colors even in non-ansi mode if strictly passed
327327
}
328+
// array key will exist if fail-fast option is used
329+
if (array_key_exists('fail-fast', $userOptions)) {
330+
$userOptions['fail-fast'] = (int) $this->options['fail-fast'] ?: 1;
331+
}
328332

329333
$suite = $input->getArgument('suite');
330334
$test = $input->getArgument('test');

src/Codeception/Subscriber/FailFast.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Codeception\Subscriber;
33

44
use Codeception\Event\SuiteEvent;
5+
use Codeception\Event\TestEvent;
56
use Codeception\Events;
67
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
78

@@ -10,12 +11,33 @@ class FailFast implements EventSubscriberInterface
1011
use Shared\StaticEvents;
1112

1213
public static $events = [
13-
Events::SUITE_BEFORE => 'stopOnFail',
14+
Events::TEST_FAIL => 'stopOnFail',
15+
Events::TEST_ERROR => 'stopOnFail',
16+
Events::SUITE_BEFORE => 'cacheSuite'
1417
];
1518

16-
public function stopOnFail(SuiteEvent $e)
19+
private $failureCount = 0;
20+
21+
private $stopFailureCount;
22+
23+
private $suiteCache;
24+
25+
public function __construct($stopFailureCount)
26+
{
27+
$this->stopFailureCount = (int) $stopFailureCount;
28+
}
29+
30+
public function cacheSuite(SuiteEvent $e)
1731
{
18-
$e->getResult()->stopOnError(true);
19-
$e->getResult()->stopOnFailure(true);
32+
$this->suiteCache = $e->getResult();
33+
}
34+
35+
public function stopOnFail(TestEvent $e)
36+
{
37+
$this->failureCount++;
38+
39+
if ($this->failureCount >= $this->stopFailureCount) {
40+
$this->suiteCache->stop();
41+
}
2042
}
2143
}

tests/cli/RunCest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ public function runOneGroupWithDataProviders(\CliGuy $I)
296296
$I->seeInShellOutput("OK");
297297
}
298298

299-
public function runTestWithFailFast(\CliGuy $I)
299+
public function runTestWithFailFastDefault(\CliGuy $I)
300300
{
301301
$I->executeCommand('run unit --skip-group error --no-exit');
302302
$I->seeInShellOutput('FailingTest: Me');
@@ -306,6 +306,14 @@ public function runTestWithFailFast(\CliGuy $I)
306306
$I->dontSeeInShellOutput("PassingTest: Me");
307307
}
308308

309+
public function runTestWithFailFastCustom(\CliGuy $I)
310+
{
311+
$I->executeCommand('run unit tests/unit/FailingTest.php --fail-fast=2 --no-exit');
312+
$I->seeInShellOutput('There were 2 failures');
313+
$I->executeCommand('run unit tests/unit/FailingTest.php --no-exit');
314+
$I->seeInShellOutput('There were 3 failures');
315+
}
316+
309317
public function runWithCustomOutputPath(\CliGuy $I)
310318
{
311319
$I->executeCommand('run dummy --xml myverycustom.xml --html myownhtmlreport.html');

tests/data/claypit/tests/unit/FailingTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,13 @@ public function testMe()
77
$this->assertFalse(true);
88
}
99

10+
public function testMeTwo()
11+
{
12+
$this->assertFalse(true);
13+
}
14+
15+
public function testMeThree()
16+
{
17+
$this->assertFalse(true);
18+
}
1019
}

0 commit comments

Comments
 (0)