Skip to content

Commit

Permalink
Add support for Rule Backgrounds. (#2668)
Browse files Browse the repository at this point in the history
* 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
clrudolphi and gasparnagy authored Dec 15, 2022
1 parent add15ba commit 8e0e7d4
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 3 deletions.
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

0 comments on commit 8e0e7d4

Please sign in to comment.