How to write Loop in Cucumber grammar

12,574 views
Skip to first unread message

Samar Mukherjee

unread,
Feb 9, 2016, 8:58:28 AM2/9/16
to Cukes
Can any one help me to write a cucumber grammar which will work as Loop similar in any programming language.

Below may be the steps what I want:

Step1: Given I loop 10 times
Step2: When I send .....
Step3: Then I receive....
Step4: Given I end Loop


In the above 4 steps,  step #2 and #3 will run 10 times..


//Samar

Thomas Sundberg

unread,
Feb 9, 2016, 9:37:13 AM2/9/16
to cu...@googlegroups.com
Hi!
This is not something you do in Gherkin. What you, most probably,
would like to do is to specify a behaviour and then implement the
specific loop in the supporting steps definition. Or in a support
class used by the steps.

Gherkin as such is not a Turing complete language and does not support
repetition. Or conditions for that matter. Those details should be
implemented in the code that connects Gherkin with the system you are
defining the behaviour for.

What you would like to consider is how do you want the system to
behave? What value does it bring to a user? Looping is an
implementation detail. The reason why you would like the loop to be
executed is what you would like to express using Gherkin.

/Thomas


>
> //Samar
>
> --
> Posting rules: http://cukes.info/posting-rules.html
> ---
> You received this message because you are subscribed to the Google Groups
> "Cukes" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to cukes+un...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Thomas Sundberg
M. Sc. in Computer Science

Mobile: +46 70 767 33 15
Blog: http://thomassundberg.wordpress.com/
Twitter: @thomassundberg

Better software through faster feedback

Eric Kessler

unread,
Feb 9, 2016, 11:27:46 AM2/9/16
to Cukes, t...@kth.se

Yeah, what Thomas said.

There may be a loop involved in the behavior that you are describing:

    Given I want to slam my service
    When I send "10000" requests in rapid succession
    Then they were all serviced within a second

but the coding for the loop is in the step definition of the looping step and not directly in the test itself.



Eric Kessler

Samar Mukherjee

unread,
Feb 12, 2016, 6:31:46 AM2/12/16
to Cukes, t...@kth.se
Thanks Thomas for your response. 
So we are using Cucumber to test different protocol level calls i.e. Diameter, RADIUA, SOAP etc

Just for an example here for repetitive diameter call we are now writing same step again and again so feature file become clumsy and tough to maintain. For that we was trying to implement the loop call.

One good news that finally by using Spring AOP I am able to implement that and now we can write feature file with loop grammar. And Loop grammar will just repeat all the steps till the loop end grammar reached.

 Given I loop 500 times
 When I send .....
  Then I receive ....
Given I end loop
 

Regards,
Samar

Andrew Premdas

unread,
Feb 12, 2016, 11:25:17 AM2/12/16
to cu...@googlegroups.com
On 12 February 2016 at 11:31, Samar Mukherjee <samarmu...@gmail.com> wrote:
Thanks Thomas for your response. 
So we are using Cucumber to test different protocol level calls i.e. Diameter, RADIUA, SOAP etc

Just for an example here for repetitive diameter call we are now writing same step again and again so feature file become clumsy and tough to maintain. For that we was trying to implement the loop call.

One good news that finally by using Spring AOP I am able to implement that and now we can write feature file with loop grammar. And Loop grammar will just repeat all the steps till the loop end grammar reached.

 Given I loop 500 times
 When I send .....
  Then I receive ....
Given I end loop

Why don't you just write

    When I call foo 500 times
    Then I should receive bar 500 times
 



--
------------------------
Andrew Premdas

Samar Mukherjee

unread,
Feb 15, 2016, 1:53:01 AM2/15/16
to Cukes
I think this implementation will be possible for only one single step. But I was looking for multiple steps within the loop and loop grammar will be generic implementation and will execute any step within it.

As I have mentioned earlier in this thread that I have implemented this using Sprint aspect programming. And I have defined a pointcut  for all the glue functions and holding call/joinpoint for the next all glue call. and once end loop step has been reached.

for (int i = 1; i < loopSize; i++) {
LOGGER.debug("\n------------------------------- Loop call {}/{}---------------------------------",i,loopSize);
for (ProceedingJoinPoint loopCall : loopCallList) {
try {
loopCall.proceed();
LOGGER.debug("Call proceeded");
} catch (Throwable e) {

LOGGER.error("Loop Execution failed. Error:"+ e.getMessage());
// LOGGER.error("\n------------------------------- Loop call {}/{} End ---------------------------------",i,loopSize);
// throw new AssertionError(e.getMessage());
// throw new PendingException(e.getMessage());
} finally {
//TODO
}
}
LOGGER.debug("\n------------------------------- Loop call {}/{} End ---------------------------------",i,loopSize);
}

Regards,
Samar

Andrew Premdas

unread,
Feb 15, 2016, 3:27:49 AM2/15/16
to cu...@googlegroups.com
On 15 February 2016 at 06:53, Samar Mukherjee <samarmu...@gmail.com> wrote:
I think this implementation will be possible for only one single step. But I was looking for multiple steps within the loop and loop grammar will be generic implementation and will execute any step within it.

You think this because you are trying to program in your scenarios, This is not what scenarios are for. Scenarios are for describing simply WHAT you are doing. You do this by using 'abstraction'. The way abstraction works is that for any complicatead process, you gather the essence of that process into a name and then use that name to represent the whole process. For example

I boil some water
I get a cup
I place some tea in it
I pour the boling water into the cup
I wait 3 minutes
I add some milk

Can be abstracted into

I make a cup of tea.

Once you understand the power of abstraction you should quickly realise that any single step can repreent any level of complexity. The art of using Cucumber is choosing your language so your steps communicate clearly WHAT you are doing, without exposing in any way HOW you are doing it.. Cucumber features demand that you hide complexity, by using abstraction and discovering the names and phrases that simply describe WHAT you are doing. The implementation part of this art is pushing all the complexity about how you do things further down the stack. The lower down the detail is the better.

Your stack (roughly) is

Scenarios
Step Definitions
Helper code for step definitions
Your program

The lower down you push all this cruft about loggin looping etc. the easier your work will be with Cucumber. Ideally all tis stuff should be in the last two levels.

All best

Andrew

Roberto Lo Giacco

unread,
Feb 15, 2016, 3:34:47 AM2/15/16
to Cukes


Il giorno lunedì 15 febbraio 2016 07:53:01 UTC+1, Samar Mukherjee ha scritto:
I think this implementation will be possible for only one single step. But I was looking for multiple steps within the loop and loop grammar will be generic implementation and will execute any step within it.

It's not that difficult to understand if you just try to read without focusing on what you are looking for only: Cucumber IS NOT a testing framework.

Creating a generic sequence of steps does make absolutely no sense in Cucumber or Gherkin, which means either you are misunderstanding what has been asked you to do or who gave you the goal doesn't understand what he is asking for.

Steps should not be generic, on the other end they should specific enough to avoid ambiguity, each and every one of them.

This does not mean you cannot have looping code, but the loop should not be exposed as such to the feature reader: the above example is perfect, you have to take your time and understand what we are trying to say because it is not about how to solve your problem, instead we are telling you your problem is caused by a misuse or misunderstanding of what Cucumber (and Gherking) is meant for.

There are plenty of resources out there, most of them written by expert people or even the creators of Cucumber, which I suggest you read.

Regards,
   Roberto

Samar Mukherjee

unread,
Feb 16, 2016, 2:30:30 PM2/16/16
to Cukes
Thank you all for all your suggestions and examples. My requirement may be sounds like nonsense but I would like to explain it so that I get more suggestions and avoid misunderstanding here .

In my implementation I have tried to expose minimum Gherkin grammar for maximum test case coverage so with above example of Andrew(Tea making) may be I have glue implementation like

Then I add (.*) => here you can add Tea or Coffee or Coco and prepare your drink
Also you can add Sugar by using the above step. But for Tea it may be 2 spoon and for Coffee it may be 3 spoon. So, here by writing the loop Gherkin grammar Sugar addition can be controlled and you can make your drink as you need.

Actually we have used cucumber to read the feature files(our test cases) and via glue implementation passed the call/control to our abstracted protocol simulator drivers i.e. Diameter, RADIUS, SOAP, REST, SMPP, SMTP, SCTP etc . And specifically for few protocol call validation loop or conditional check was essential.

I will also request everyone not to break the best practices of cucumber and this may be a typical requirement very specific to our case and may not be generic to cucumber framework.

//Samar 

Roberto Lo Giacco

unread,
Feb 16, 2016, 6:08:05 PM2/16/16
to cu...@googlegroups.com
Samar, this is last time I'm going to repeat this to you: feature files are not test cases. They are not meant to be. Cucumber is not a testing framework.

It's not a matter of philosophy os something intangible, I'm not trying to be fussy here, I'm telling you you are trying to use a fork to pour coffee instead of a spoon! Can you pour coffee with a fork? Yes, but it will require you a lot more time and effort than using a spoon! And asking to the fork makers how you can use it as a spoon makes ABSOLUTELY no sense whatsoever.

Once again, in your steps you are describing HOW you make a coffee (take 2 spoons of coffee, 3 ounces of water, warm it up, filter, pour then enjoy it). 
Gherkin and Cucumber are meant to describe WHAT you want, not HOW to do it. 
So in the above example, it is "Since I want some coffee, then I ask for one cup at the caffetteria and I should get one". 
Do you want a tea instead? "Since I want some tea, then I ask for one cup at the caffetteria and I should get one".
Do you want a steak? "Since I want some beef, then I ask for one steak at the steak house and I should get a very big one".

Once again: it's about WHAT, not about HOW.

Is it ok to use Gherkin to DESCRIBE a REST API? Sure it IS! The only testing facility provided by Cucumber is (read it twice) the possibility to programmatically verify the system you described matches with the system you have implemented.

If you don't get the difference, or if you believe it's metaphysics, philosophy or something abstract then put Gherkin aside and move to a testing framework.

Your wasn't breaking "best practices", it is improper use of the tool.

--
You received this message because you are subscribed to a topic in the Google Groups "Cukes" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cukes/cs9fxMRbP3k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cukes+un...@googlegroups.com.

Andrew Premdas

unread,
Feb 17, 2016, 7:29:50 AM2/17/16
to cu...@googlegroups.com
On 16 February 2016 at 19:30, Samar Mukherjee <samarmu...@gmail.com> wrote:
Thank you all for all your suggestions and examples. My requirement may be sounds like nonsense but I would like to explain it so that I get more suggestions and avoid misunderstanding here .

In my implementation I have tried to expose minimum Gherkin grammar for maximum test case coverage so with above example of Andrew(Tea making) may be I have glue implementation like

Then I add (.*) => here you can add Tea or Coffee or Coco and prepare your drink
Also you can add Sugar by using the above step. But for Tea it may be 2 spoon and for Coffee it may be 3 spoon. So, here by writing the loop Gherkin grammar Sugar addition can be controlled and you can make your drink as you need.

In my example I abstracted the Gherkin too "I make a cup of tea". The point of this was to move the details of tea making further down the stack. What you are doing is bringing the details back up the stack into the scenario. This is precisely the wrong thing to do. If you need to make a variety of drinks then do a scenario for each one

    I make a cup of tea
    I make a cup of coffee
    I make a triple choc mocha latte

where each scenario is about WHAT drink we are making.

Or perhaps get even more abstract and say

    I make a drink

None of these scenarios are about HOW we make the drink. HOW has no place in scenarios, each time you put back the HOW into your scenarios you are misusing Cucumber and making your life more difficult. When you write scenarios you have to stop thinking like a tester and start thinking like a business person.


All best

Andrew


Actually we have used cucumber to read the feature files(our test cases) and via glue implementation passed the call/control to our abstracted protocol simulator drivers i.e. Diameter, RADIUS, SOAP, REST, SMPP, SMTP, SCTP etc . And specifically for few protocol call validation loop or conditional check was essential.

I will also request everyone not to break the best practices of cucumber and this may be a typical requirement very specific to our case and may not be generic to cucumber framework.

//Samar 

On Monday, 15 February 2016 14:04:47 UTC+5:30, Roberto Lo Giacco wrote:


Il giorno lunedì 15 febbraio 2016 07:53:01 UTC+1, Samar Mukherjee ha scritto:
I think this implementation will be possible for only one single step. But I was looking for multiple steps within the loop and loop grammar will be generic implementation and will execute any step within it.

It's not that difficult to understand if you just try to read without focusing on what you are looking for only: Cucumber IS NOT a testing framework.

Creating a generic sequence of steps does make absolutely no sense in Cucumber or Gherkin, which means either you are misunderstanding what has been asked you to do or who gave you the goal doesn't understand what he is asking for.

Steps should not be generic, on the other end they should specific enough to avoid ambiguity, each and every one of them.

This does not mean you cannot have looping code, but the loop should not be exposed as such to the feature reader: the above example is perfect, you have to take your time and understand what we are trying to say because it is not about how to solve your problem, instead we are telling you your problem is caused by a misuse or misunderstanding of what Cucumber (and Gherking) is meant for.

There are plenty of resources out there, most of them written by expert people or even the creators of Cucumber, which I suggest you read.

Regards,
   Roberto

--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages