Skip to content

Commit

Permalink
Added optional value to fail-fast option
Browse files Browse the repository at this point in the history
  • Loading branch information
Verest committed Nov 9, 2021
1 parent f8881df commit 29cd2d1
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Codeception/Codecept.php
Expand Up @@ -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']) {
Expand Down
8 changes: 6 additions & 2 deletions src/Codeception/Command/Run.php
Expand Up @@ -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`
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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');
Expand Down
30 changes: 26 additions & 4 deletions src/Codeception/Subscriber/FailFast.php
Expand Up @@ -2,6 +2,7 @@
namespace Codeception\Subscriber;

use Codeception\Event\SuiteEvent;
use Codeception\Event\TestEvent;
use Codeception\Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand All @@ -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();
}
}
}
10 changes: 9 additions & 1 deletion tests/cli/RunCest.php
Expand Up @@ -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');
Expand All @@ -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');
Expand Down
9 changes: 9 additions & 0 deletions tests/data/claypit/tests/unit/FailingTest.php
Expand Up @@ -7,4 +7,13 @@ public function testMe()
$this->assertFalse(true);
}

public function testMeTwo()
{
$this->assertFalse(true);
}

public function testMeThree()
{
$this->assertFalse(true);
}
}

0 comments on commit 29cd2d1

Please sign in to comment.