-
Notifications
You must be signed in to change notification settings - Fork 754
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Rule Backgrounds. (#2668)
* Add support for Rule Backgrounds. Preliminary commit. Lacks tests. * Add support for Rule Backgrounds.Revised Commit to simplify approach and code. Lacks tests. * Refactored signature of method: UnitTestMethodGenerator.GenerateTestMethodBody to accept a ScenarioDefinitionInFeatureFile instead of an AST Scenario. This allows simpler tracking of the relationship between Scenario and Rule. Refactored Rule Background support in ScenarioPartHelper to simplify the code and accept changes suggested during PR review. * Adding a feature file in the Specs tests to demonstrate a Feature with multiple Rules, each having a Background; the Background steps should only be executed for the Scenario(s) within their respective Rule. * Further simplification of Rule support in SPH by merging two private methods together. * Corrected feature's binding code. Tests now pass. * Updating changelog * Reverting this feature back to the version present in SpecFlowOSS/SpecFlow. * Refactored/revised to improve readabliity and formatting Co-authored-by: Gáspár Nagy <[email protected]>
- Loading branch information
1 parent
add15ba
commit 8e0e7d4
Showing
5 changed files
with
184 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
Tests/TechTalk.SpecFlow.Specs/Features/Execution/RuleBackgroundExecution.feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
@gherkin6 | ||
Feature: Rule Background Steps Are Added to Scenarios Belonging to Rules | ||
|
||
Scenario: Should be able to execute scenarios in Rules that have backgrounds | ||
Given the following binding class | ||
""" | ||
using TechTalk.SpecFlow; | ||
[Binding] | ||
public class RuleStepsForFirstScenario | ||
{ | ||
[Given("a background step")] | ||
public void TheBackgroundStep() | ||
{ | ||
global::Log.LogStep(); | ||
} | ||
[When("I do something")] | ||
public void WhenSomethingDone() | ||
{ | ||
global::Log.LogStep(); | ||
} | ||
} | ||
""" | ||
|
||
Given there is a feature file in the project as | ||
""" | ||
Feature: Simple Feature | ||
Rule: A Rule | ||
Background: rule background | ||
Given a background step | ||
Scenario: Scenario for the first rule | ||
When I do something | ||
""" | ||
|
||
When I execute the tests | ||
Then the binding method 'TheBackgroundStep' is executed | ||
|
||
|
||
Scenario: Should be able to execute backgrounds from multiple Rules | ||
Given the following binding class | ||
""" | ||
using System; | ||
using TechTalk.SpecFlow; | ||
internal class StepInvocationTracker | ||
{ | ||
private bool first_background_step_executed = false; | ||
private bool second_backgroun_step_executed = false; | ||
public void MarkFirstStepAsExecuted() => first_background_step_executed = true; | ||
public void MarkSecondStepAsExecuted() => second_backgroun_step_executed = true; | ||
public bool WasFirstStepInvoked => first_background_step_executed; | ||
public bool WasSecondStepInvoked => second_backgroun_step_executed; | ||
} | ||
[Binding] | ||
public class RuleStepsForFeatureContainingMultipleRules | ||
{ | ||
private StepInvocationTracker invocationTracker = new StepInvocationTracker(); | ||
[Given("a background step for the first rule")] | ||
public void GivenaFirst() | ||
{ | ||
global::Log.LogStep(); | ||
invocationTracker.MarkFirstStepAsExecuted(); | ||
} | ||
[Then("the first background step was executed")] | ||
public void ThenFirstOfTwoWasExecuted() | ||
{ | ||
global::Log.LogStep(); | ||
if (!invocationTracker.WasFirstStepInvoked) | ||
{ | ||
throw new ApplicationException("First Background Step should have been executed"); | ||
} | ||
} | ||
[Then("the step from the first background was not executed")] | ||
public void ThenFirstWasNotExecuted() | ||
{ | ||
global::Log.LogStep(); | ||
if (invocationTracker.WasFirstStepInvoked) | ||
{ | ||
throw new ApplicationException("First Background Step should not have been executed"); | ||
} | ||
} | ||
[Given("a background step for the second rule")] | ||
public void GivenSecondBackgroundStepExecuted() | ||
{ | ||
global::Log.LogStep(); | ||
invocationTracker.MarkSecondStepAsExecuted(); | ||
} | ||
[Then("the second background step was executed")] | ||
public void ThenSecondWasExecuted() | ||
{ | ||
global::Log.LogStep(); | ||
if (!invocationTracker.WasSecondStepInvoked) | ||
{ | ||
throw new ApplicationException("Second Background Step should have been executed"); | ||
} | ||
} | ||
[Then("the step from the second background was not executed")] | ||
public void ThenSecondWasNotExecuted() | ||
{ | ||
global::Log.LogStep(); | ||
if (invocationTracker.WasSecondStepInvoked) | ||
{ | ||
throw new ApplicationException("Second Background Step should not have been executed"); | ||
} | ||
} | ||
} | ||
""" | ||
Given there is a feature file in the project as | ||
""" | ||
Feature: A Feature with multiple Rules, each with their own Backgrounds | ||
Rule: first Rule | ||
Background: first Rule's background | ||
Given a background step for the first rule | ||
Scenario: Scenario for the first rule | ||
Then the first background step was executed | ||
And the step from the second background was not executed | ||
Rule: second Rule | ||
Background: second Rule's background | ||
Given a background step for the second rule | ||
Scenario:Scenario for the second rule | ||
Then the second background step was executed | ||
And the step from the first background was not executed | ||
""" | ||
|
||
When I execute the tests | ||
Then all tests should pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters