Details
Description
I'm working on a simplification of the current parameterized scenario solution. With this I want to:
- avoid the need of defining two different patterns for steps, one for normal scenarios, and another for parameterized scenarios.
- be able to use column names in Examples tables that differ from the parameter names that were specified in the step annotation
- avoid having to annotate fields with @Named in parameterized step methods
Take the following steps and scenario, for example:
public class ParameterizedStory extends JUnitStory { public static class ParamsSteps { private String user; private Set<String> products; // null means no cart... @BeforeScenario public void init() { user = null; products = null; } @Given("$user is logged in") public void loggedIn(String user) { this.user = user; } @Given("$customer has a cart") public void aCustomerHasACart(String customer) { assertThat(user, notNullValue()); assertThat(customer, equalTo(user)); if (products == null) products = new TreeSet<String>(); } @When("a $product is added to the cart") public void aProductIsAddedToCart(String product) { assertThat(products, notNullValue()); products.add(product); } @Then("cart contains $product") public void cartContains(String product) { assertThat(products, notNullValue()); assertTrue(products.contains(product)); } } public ParameterizedStory() { // Making sure this doesn't output to the build while it's running useConfiguration(new MostUsefulConfiguration() .useStoryReporterBuilder(new StoryReporterBuilder() .withFormats(Format.CONSOLE) .withFailureTrace(true) .withFailureTraceCompression(false)) .usePendingStepStrategy(new FailingUponPendingStep()) .useFailureStrategy(new RethrowingFailure())); } @Override public List<CandidateSteps> candidateSteps() { return new InstanceStepsFactory(configuration(), new ParamsSteps()).createCandidateSteps(); } }
Scenario: Use flexible parameters with examples table Given <client> is logged in And <client> has a cart When a <item> is added to the cart Then cart contains <item> Examples: |client|item| |Rui|chocolate| |Figueira|car|
The "Given $user is logged in" step pattern will match "Given <client> is logged in", and user parameter will be set to client column values.
The main idea is that <client>, instead of represent a named parameter, represents a placeholder, and that placeholder should be replaced with whether value exists for it in the examples table. Then, after replacing the placeholder with the corresponding value, it will then match with the given step pattern, and $user will be set to the correct value.
I have a working version available at https://github.com/ruifigueira/jbehave-core/commit/a4107a3ac899c4b952746781f785cba9a97c659c, that can be used to test the scenario above.
Activity
Field | Original Value | New Value |
---|---|---|
Attachment | StepCreator.java [ 58919 ] |
Summary | Parametrised scenarios can be simplified | Allow parametrisation of scenarios by delimited names |
Assignee | Mauro Talevi [ maurotalevi ] | |
Fix Version/s | 3.6 [ 17721 ] |
Status | Open [ 1 ] | In Progress [ 3 ] |
Status | In Progress [ 3 ] | Resolved [ 5 ] |
Resolution | Fixed [ 1 ] |
I forgot to mention two other advantages of this simplification: