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 all 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,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
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ Scenario: Should be able to execute a simple passing scenario
When I execute the tests
Then the execution summary should contain
| Total |
| 5 |
| 5 |
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