Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/option rerun only #1466

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
180 changes: 180 additions & 0 deletions features/rerun-only.feature
Original file line number Diff line number Diff line change
@@ -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:
"""
<?php

use Behat\Behat\Context\Context;

class FeatureContext implements Context
{
private $apples = 0;
private $parameters;

public function __construct(array $parameters = array()) {
$this->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 <ate> apples
And I found <found> apples
Then I should have <result> 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 <ate> apples
And I found <found> apples
Then I should have <result> 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:
"""
"""
7 changes: 5 additions & 2 deletions src/Behat/Behat/Tester/Cli/RerunController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
);
}

/**
Expand All @@ -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());
Expand Down