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

Add support for Rule Backgrounds. #2668

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions TechTalk.SpecFlow.Generator/Generation/ScenarioPartHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,37 @@ public void SetupFeatureBackground(TestClassGenerationContext generationContext)
backgroundMethod.Statements.AddRange(statements.ToArray());

}
#region Rule Background Support

public void GenerateRuleBackgroundStepsApplicableForThisScenario(TestClassGenerationContext generationContext, ScenarioDefinitionInFeatureFile scenarioDefinition, List<CodeStatement> statementsWhenScenarioIsExecuted)
{
if (scenarioDefinition.Rule != null)
{
var rule = scenarioDefinition.Rule;
IEnumerable<CodeStatement> ruleBackgroundStatements = GenerateBackgroundStatementsForRule(generationContext, rule);
statementsWhenScenarioIsExecuted.AddRange(ruleBackgroundStatements);
}
}

private IEnumerable<CodeStatement> GenerateBackgroundStatementsForRule(TestClassGenerationContext context, Rule rule)
{
var background = rule.Children.OfType<Background>().FirstOrDefault();

if (background == null) return new List<CodeStatement>();

var statements = new List<CodeStatement>();
using (new SourceLineScope(_specFlowConfiguration, _codeDomHelper, statements, context.Document.SourceFilePath, background.Location))
{
foreach (var step in background.Steps)
{
GenerateStep(context, statements, step, null);
}
}

return statements;
}

#endregion

public void GenerateStep(TestClassGenerationContext generationContext, List<CodeStatement> statements, Step gherkinStep, ParameterSubstitution paramToIdentifier)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private void GenerateTestBody(

GenerateScenarioInitializeCall(generationContext, scenarioDefinition, testMethod);

GenerateTestMethodBody(generationContext, scenarioDefinition, testMethod, paramToIdentifier, feature);
GenerateTestMethodBody(generationContext, scenarioDefinitionInFeatureFile, testMethod, paramToIdentifier, feature);

GenerateScenarioCleanupMethodCall(generationContext, testMethod);
}
Expand Down Expand Up @@ -238,8 +238,10 @@ private void AddVariableForArguments(CodeMemberMethod testMethod, ParameterSubst
}
}

internal void GenerateTestMethodBody(TestClassGenerationContext generationContext, StepsContainer scenario, CodeMemberMethod testMethod, ParameterSubstitution paramToIdentifier, SpecFlowFeature feature)
internal void GenerateTestMethodBody(TestClassGenerationContext generationContext, ScenarioDefinitionInFeatureFile scenarioDefinition, CodeMemberMethod testMethod, ParameterSubstitution paramToIdentifier, SpecFlowFeature feature)
{
var scenario = scenarioDefinition.Scenario;

var statementsWhenScenarioIsIgnored = new CodeStatement[] { new CodeExpressionStatement(CreateTestRunnerSkipScenarioCall()) };

var callScenarioStartMethodExpression = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), generationContext.ScenarioStartMethod.Name);
Expand All @@ -265,6 +267,7 @@ internal void GenerateTestMethodBody(TestClassGenerationContext generationContex
}
}

_scenarioPartHelper.GenerateRuleBackgroundStepsApplicableForThisScenario(generationContext, scenarioDefinition, statementsWhenScenarioIsExecuted);

foreach (var scenarioStep in scenario.Steps)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
@gherkin6
Feature: Rule Background Steps Execution

Scenario: Should be able to execute scenarios in Rules that have backgrounds
Given the following binding class
"""
using TechTalk.SpecFlow;

[Binding]
public class RuleSteps1
{

[Given("something first as background")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this formatting only broken on GitHub or also in VS?
If so, please fix it. Thanks

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is really hard to read for me in this style

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SabotageAndi I have rewritten this feature file in an attempt to improve its readability. Please let me know what you think.
RuleBackgroundExecution.feature

public void GivenSomethingFirst() {
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: first rule
Background: first rule background
Given something first as background

Scenario: Scenario for the first rule
When I do something
"""


When I execute the tests
Then the binding method 'GivenSomethingFirst' is executed


Scenario: Should be able to execute backgrounds from multiple Rules
Given the following binding class
"""
using TechTalk.SpecFlow;

[Binding]
public class RuleSteps2
{
private bool first_background_executed = false;
private bool second_background_executed = false;

[Given("a first thing as background")]
public void GivenaFirst() {
global::Log.LogStep();
first_background_executed = true;
}

[Then("the first of two background item was executed")]
public void ThenFirstOfTwoWasExecuted() {
global::Log.LogStep();
if (!first_background_executed) {
throw new ApplicationException("First Background Step should have been executed");
}
}

[Then("the first background item was not executed")]
public void ThenFirstWasNotExecuted() {
global::Log.LogStep();
if (first_background_executed) {
throw new ApplicationException("First Background Step should not have been executed");
}
}

[Given("something second as background")]
public void GivenSomethingSecond() {
global::Log.LogStep();
second_background_executed = true;
}

[Then("the second background item was executed")]
public void ThenSecondWasExecuted() {
global::Log.LogStep();
if (!second_background_executed) {
throw new ApplicationException("Second Background Step should have been executed");
}
}

[Then("the second background item was not executed")]
public void ThenSecondWasNotExecuted() {
global::Log.LogStep();
if (second_background_executed) {
throw new ApplicationException("Second Background Step should not have been executed");
}
}

[When("I do something")]
public void WhenSomethingDone() {
global::Log.LogStep();
}

}
"""
Given there is a feature file in the project as
"""
Feature: Simple Feature
Rule: first rule
Background: first rule background
Given a first thing as background

Scenario: Scenario for the first rule
When I do something
Then the first of two background item was executed
And the second background item was not executed

Rule: second rule
Background: bg for second rule
Given something second as background

Scenario:Scenario for the second rule
When I do something
Then the second background item was executed
And the first background item was not executed

"""

When I execute the tests
Then all tests should pass
21 changes: 21 additions & 0 deletions Tests/TechTalk.SpecFlow.Specs/Features/Parser/RuleSupport.feature
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,24 @@ Scenario: Should be able to execute a simple passing scenario
Then the execution summary should contain
| Total |
| 5 |



Scenario: Should be able to execute scenarios in Rules that have backgrounds
Given there is a feature file in the project as
"""
Feature: Simple Feature
Rule: first rule
Background: first rule background
Given something

Scenario: Scenario for the first rule
When I do something

"""
Given all steps are bound and pass
When I execute the tests
Then the execution summary should contain
clrudolphi marked this conversation as resolved.
Show resolved Hide resolved
| Total |
| 1 |

1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Features:
+ Support Rule tags (can be used for hook filters, scoping and access through 'ScenarioInfo.CombinedTags')
+ Support for async step argument transformations. Fixes #2230
+ Support for ValueTask and ValueTask<T> binding methods (step definitions, hooks, step argument transformations)
+ Rules now support Background blocks
+ Collect binding errors (type load, binding, step definition) and report them as exception when any of the tests are executed.

Changes:
Expand Down