diff --git a/features/rerun-only.feature b/features/rerun-only.feature new file mode 100644 index 000000000..77dc9440d --- /dev/null +++ b/features/rerun-only.feature @@ -0,0 +1,180 @@ +Feature: Rerun + In order to test only failed scenarios and exit if none is found + As a feature developer + I need to have an ability to rerun failed previously scenarios + + Background: + Given a file named "features/bootstrap/FeatureContext.php" with: + """ + parameters = $parameters; + } + + /** + * @Given /^I have (\d+) apples?$/ + */ + public function iHaveApples($count) { + $this->apples = intval($count); + } + + /** + * @When /^I ate (\d+) apples?$/ + */ + public function iAteApples($count) { + $this->apples -= intval($count); + } + + /** + * @When /^I found (\d+) apples?$/ + */ + public function iFoundApples($count) { + $this->apples += intval($count); + } + + /** + * @Then /^I should have (\d+) apples$/ + */ + public function iShouldHaveApples($count) { + PHPUnit\Framework\Assert::assertEquals(intval($count), $this->apples); + } + + /** + * @Then /^context parameter "([^"]*)" should be equal to "([^"]*)"$/ + */ + public function contextParameterShouldBeEqualTo($key, $val) { + PHPUnit\Framework\Assert::assertEquals($val, $this->parameters[$key]); + } + + /** + * @Given /^context parameter "([^"]*)" should be array with (\d+) elements$/ + */ + public function contextParameterShouldBeArrayWithElements($key, $count) { + PHPUnit\Framework\Assert::assertIsArray($this->parameters[$key]); + PHPUnit\Framework\Assert::assertEquals(2, count($this->parameters[$key])); + } + } + """ + 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 3 apples + + Scenario: Found more apples + When I found 5 apples + Then I should have 8 apples + + Scenario: Found more apples + When I found 2 apples + Then I should have 5 apples + + Scenario Outline: Other situations + When I ate apples + And I found apples + Then I should have apples + + Examples: + | ate | found | result | + | 3 | 1 | 1 | + | 0 | 4 | 8 | + | 2 | 2 | 3 | + """ + + Scenario: Run one feature with 2 failed and 3 passing scenarios + When I run "behat --no-colors -f progress features/apples.feature" + Then it should fail with: + """ + ..F.............F.... + + --- Failed steps: + + 001 Scenario: I'm little hungry # features/apples.feature:9 + Then I should have 3 apples # features/apples.feature:11 + Failed asserting that 2 matches expected 3. + + 002 Example: | 0 | 4 | 8 | # features/apples.feature:29 + Then I should have 8 apples # features/apples.feature:24 + Failed asserting that 7 matches expected 8. + + 6 scenarios (4 passed, 2 failed) + 21 steps (19 passed, 2 failed) + """ + + Scenario: Rerun only failed scenarios + Given I run "behat --no-colors -f progress features/apples.feature" + When I run "behat --no-colors -f progress features/apples.feature --rerun-only" + Then it should fail with: + """ + ..F...F + + --- Failed steps: + + 001 Scenario: I'm little hungry # features/apples.feature:9 + Then I should have 3 apples # features/apples.feature:11 + Failed asserting that 2 matches expected 3. + + 002 Example: | 0 | 4 | 8 | # features/apples.feature:29 + Then I should have 8 apples # features/apples.feature:24 + Failed asserting that 7 matches expected 8. + + 2 scenarios (2 failed) + 7 steps (5 passed, 2 failed) + """ + + Scenario: Fixing scenario removes it from the rerun log + Given I run "behat --no-colors -f progress features/apples.feature" + And there is 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 + + Scenario: Found more apples + When I found 5 apples + Then I should have 8 apples + + Scenario: Found more apples + When I found 2 apples + Then I should have 5 apples + + Scenario Outline: Other situations + When I ate apples + And I found apples + Then I should have apples + + Examples: + | ate | found | result | + | 3 | 1 | 1 | + | 0 | 4 | 7 | + | 2 | 2 | 3 | + """ + When I run "behat --no-colors -f progress features/apples.feature" + And I run "behat --no-colors -f progress features/apples.feature --rerun-only" + Then it should pass with: + """ + """ diff --git a/src/Behat/Behat/Tester/Cli/RerunController.php b/src/Behat/Behat/Tester/Cli/RerunController.php index 93bd84ff9..bb62d8c8a 100644 --- a/src/Behat/Behat/Tester/Cli/RerunController.php +++ b/src/Behat/Behat/Tester/Cli/RerunController.php @@ -74,6 +74,9 @@ public function configure(Command $command) $command->addOption('--rerun', null, InputOption::VALUE_NONE, 'Re-run scenarios that failed during last execution.' ); + $command->addOption('--rerun-only', null, InputOption::VALUE_NONE, + 'Re-run scenarios that failed during last execution or exit if no file is found.' + ); } /** @@ -92,12 +95,12 @@ public function execute(InputInterface $input, OutputInterface $output) $this->key = $this->generateKey($input); - if (!$input->getOption('rerun')) { + if (!$input->getOption('rerun') && !$input->getOption('rerun-only')) { return; } if (!$this->getFileName() || !file_exists($this->getFileName())) { - return; + return $input->getOption('rerun-only') ? 0 : null; } $input->setArgument('paths', $this->getFileName());