[Cucumber] Common scenarios in multiple feature files and multiple steps definition file

11,722 views
Skip to first unread message

shivi malviya

unread,
Nov 22, 2013, 7:04:47 AM11/22/13
to cu...@googlegroups.com
Hi,
We have developed a test framework using Cucumber. We are facing problem when we have common scenarios in multiple feature files and each feature file has a separate step definition files.
Please find below the detailed scenario :
Feature File1 :
Scenario Outline: 02 Chosen newsroom stored in cookie
    Given user visiting the newsroom page
    When <region> newsroom is selected
    Then selection shall be stored in cookie <cookieValue>


Feature File2 :
Scenario Outline: 03 Filters on newsitem listing in newsroom
    Given user visiting the newsroom page
    When a <tagfilter> is selected
    Then only news items marked with this <tagfilter> filter should be displayed
    When user clicks link "View Older News"
    Then more 25 news items are appended to news list

So the lines highlighted in Yellow are common lines used in both feature files, now we are creating one step definition file for each feature file.
Now when i am declaring step definition for highlighted step in both step definition file, it is giving me duplicate step definition error, and when I am keeping it only in one step definition file, it is giving me Null Pointer exception.
Another thing which I tried is to extend step definition file , but then I got error it is not possible to extend the step definition files.

How should i reuse my step definitions in other step definition file when we some some common scenario lines in multiple feature files ?

Please suggest.

Thanks,
Shivi

aslak hellesoy

unread,
Nov 22, 2013, 7:06:47 AM11/22/13
to Cucumber Users
On Fri, Nov 22, 2013 at 12:04 PM, shivi malviya <malviy...@gmail.com> wrote:
Hi,
We have developed a test framework using Cucumber. We are facing problem when we have common scenarios in multiple feature files and each feature file has a separate step definition files.
Please find below the detailed scenario :
Feature File1 :
Scenario Outline: 02 Chosen newsroom stored in cookie
    Given user visiting the newsroom page
    When <region> newsroom is selected
    Then selection shall be stored in cookie <cookieValue>


Feature File2 :
Scenario Outline: 03 Filters on newsitem listing in newsroom
    Given user visiting the newsroom page
    When a <tagfilter> is selected
    Then only news items marked with this <tagfilter> filter should be displayed
    When user clicks link "View Older News"
    Then more 25 news items are appended to news list

So the lines highlighted in Yellow are common lines used in both feature files, now we are creating one step definition file for each feature file.

Don't do that. Step definitions are global.
 
Now when i am declaring step definition for highlighted step in both step definition file, it is giving me duplicate step definition error, and when I am keeping it only in one step definition file, it is giving me Null Pointer exception.
Another thing which I tried is to extend step definition file , but then I got error it is not possible to extend the step definition files.

How should i reuse my step definitions in other step definition file when we some some common scenario lines in multiple feature files ?

Please suggest.

Thanks,
Shivi

--
-- Rules --
 
1) Please prefix the subject with [Ruby], [JVM] or [JS].
2) Please use interleaved answers http://en.wikipedia.org/wiki/Posting_style#Interleaved_style
3) If you have a question, don't reply to an existing message. Start a new topic instead.
 
You received this message because you are subscribed to the Google Groups Cukes group. To post to this group, send email to cu...@googlegroups.com. To unsubscribe from this group, send email to cukes+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/cukes?hl=en
---
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/groups/opt_out.

Anna

unread,
Dec 9, 2015, 4:05:06 PM12/9/15
to Cukes
I have been looking around to understand if Cucumber with Java has any support / way  to  REUSE common step definitions across multiple feature files / scenarios. The only way I can handle right now is changing the wording of step definition file which is NOT good and no solution. 

But its strange that I haven't got any link/reference on this after googling a lot. This should be the basic requirement when we design cucumber scenarios as there ought to be common sequence of steps that one would call across scenarios/ across feature files. 

For ex, 
- A user logs in (every scenario would need that and we do not want a new user to created with each scenario so can't put it in Background)
- Creating some prerequisite objects for a scenario to work.

Let me know if anybody has suggestions on this. 

Somehow I feel cucumber documentation needs to be more structured and improved as I do not see good documentation on cucumber site. These are some basic design/guidelines/ that each cucumber user would like to know !

Anna

unread,
Dec 13, 2015, 1:33:49 PM12/13/15
to Cukes
Did you get answer to your question, I have been looking around on this, but unfortunately no answers on this yet even after googling a lot, weird, as this is the most basic thing one would like to have using any framework! 

Cucumber users, anybody any suggestion on this?

Also wherein we have multiple feature files, how do we work on the initialization/setup code ? 

There is some data initialization that one want to do before running a feature file and that can goe into @before method.  But the issue is that there is no order in which feature files are picked, so thats very very frustrating, all initialization code across all feature files runs and that too without any specific order...which is not desired.
In other words, I am looking at what options or best strategy to do initialization wherein I have multiple feature files/multiple test classes (java classes) running scenarios of the corresponding feature. The issue is I can't extend a class that contains step defintions so I cannot reuse initilaization code across multiple test classes. 

Any help,appreciate in advance!

George Dinwiddie

unread,
Dec 13, 2015, 2:40:01 PM12/13/15
to cu...@googlegroups.com
Anna,

Any feature file can call any step definition.

It's not advisable to have one feature file dependent on another one (or
even one scenario dependent on another one). I get the feeling you're
trying to use cucumber as a test automation tool, and not as a
collaboration and functional specification too.

Perhaps if you give us some examples of what you're trying to
accomplish, we can help you start in a more productive direction.

- George
> --
> 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
> <mailto:cukes+un...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

--
----------------------------------------------------------------------
* George Dinwiddie * http://blog.gdinwiddie.com
Software Development http://www.idiacomputing.com
Consultant and Coach http://www.agilemaryland.org
----------------------------------------------------------------------

Anna

unread,
Dec 14, 2015, 3:03:05 AM12/14/15
to Cukes
Hey George. 

Thanks for response. 
You hit it right.. we are using Cucumber to write automation tests. The project has multiple feature files and as part of any automation process, we need some initialization and tear down code which needs to be run once, before any feature/scenarios run. 

Initially I put the initialization code in each of my test class (which has its feature file) under @Before method . However I realized that when you run the feature file through Runner it executes all feature file present in the features folder (despite me providing a specific feature file in the annotation like below which is another issue/bottleneck).
and so initialization code gets run multiple times per feature/test class. 

I am thinking to have a separate class SetupUtil that has static initialize method with a static flag isInitalizedOnce . I am using encapsulation now (as i can't use inheritance with step definitions) and calling SetupUtil.initialize with each of the test class @ before method.

AppTest1 -  This has a corresponding feature file apptest_1.feature
=============================

@Before

public void setUp() throws Exception  {

AppSetup.initialize();

myService = new MyService();

}

AppTest2 This has a corresponding feature file apptest_2.feature
=============================

@Before

public void setUp() throws Exception  {

AppSetup.initialize();

myService = new MyService();

anotherService = new AnotherService();

}


AppTest3 - This has a corresponding feature file apptest_3.feature
=============================

@Before

public void setUp() throws Exception  {

AppSetup.initialize();

myService = new MyService();
}
============================

AppSetup {

static User;

static boolean isInitializedOnceForAllFeatures = false;

public static void initialize() throws Exception  {

if(isInitializedOnceForAllFeatures) return;

//authentication, user creation and other configuration initialization code

intializeConfiguration():

createUsers();

isInitializedOnceForAllFeatures = true;

}


Let me know if this approach is correct or if there is any better design approach to handle this. Quick response is appreciated as I have to wrap this up quickly !! Also let me know if there is any good reference of things to keep in mind while designing automation tests using cucumber. 

II ) Another Issue I am getting is despite I refer a specific feature file in runner, it still invokes all feature file related step definitiaons and runs everthing. This is whats not expected. If I run a feature file , then it should ONLY run scenarios that are pertaining to that feature and NO other feature files should be picked. I am running via Eclipse. Right click on Runner class and run as Junit test.

@RunWith(Cucumber.class)

@CucumberOptions(features = "classpath:features/myTest.feature",  

plugin = {"pretty""html:target/cucumber-html-report""json:target/cucumber-report.json"})

public class AppRunnerTest {

}


III) Obviously, across multiple feature files there ought to be same step definitions being used. How do we handle such cases? As there is no inheritance possible I cant have a common base class defining those step defintions. As order is not guaranteed its also not clear where to keep definitions ? 
 
IV) Sometimes a step definition that we use in a scenario can be put in as @Given but in some other scenario that step definition falls under @When or @And. How do we handle such cases. There seems to be a high need for me while writing automated tests. Same step definition method cannot have multiple tags also like @Given, @And 

I appreciate if I can get quick answers on these so that I can identify if cucumber is the right choice for automation tests. I believe so , but looking forward for some quick response.

thanks
Anna

George Dinwiddie

unread,
Dec 14, 2015, 6:46:37 AM12/14/15
to cu...@googlegroups.com
Anna,

On 12/14/15 3:03 AM, Anna wrote:
> Hey George.
>
> Thanks for response.
> You hit it right.. we are using Cucumber to write automation tests.

Who reads your cucumber scenarios?

You might find
https://cucumber.io/blog/2014/03/03/the-worlds-most-misunderstood-collaboration-tool
helpful.

> The
> project has multiple feature files and as part of any automation
> process, we need some initialization and tear down code which needs to
> be run once, before any feature/scenarios run.
>
> Initially I put the initialization code in each of my test class (which
> has its feature file) under @Before method . However I realized that
> when you run the feature file through Runner it executes all feature
> file present in the features folder (despite me providing a specific
> feature file in the annotation like below which is another
> issue/bottleneck).
> and so initialization code gets run multiple times per feature/test class.

Cucumber will run only the feature file you specify, but it will load
all the step definitions. Step definitions do not match feature files.
All of your @Before methods will get run before each scenario.

See http://zsoltfabok.com/blog/2012/09/cucumber-jvm-hooks/ for more info
on how hooks work.

>
> I am thinking to have a separate class SetupUtil that has static
> initialize method with a static flag isInitalizedOnce . I am using
> encapsulation now (as i can't use inheritance with step definitions) and
> calling SetupUtil.initialize with each of the test class @ before method.

You don't need or want inheritance of step definitions, as that would
define them all multiple times.

>
> AppTest1 - This has a corresponding feature file apptest_1.feature

Organize your step definitions around the cohesiveness of the steps they
define, not what features use them.

> =============================
>
> @Before
>
> public void setUp() throws Exception {
>
> AppSetup.initialize();
>
> myService = new MyService();
>
> }
>
> AppTest2 This has a corresponding feature file apptest_2.feature
> =============================
>
> @Before
>
> public void setUp() throws Exception {
>
> AppSetup.initialize();
>
> myService = new MyService();
>
> anotherService = new AnotherService();
>
> }
>
>
> AppTest3 - This has a corresponding feature file apptest_3.feature
> =============================
>
> @Before
>
> public void setUp() throws Exception {
>
> AppSetup.initialize();
>
> myService = new MyService();
> }
> ============================
>
> AppSetup {
>
> static User;
>
> staticbooleanisInitializedOnceForAllFeatures= false;
>
> public static void initialize() throws Exception {
>
> if(isInitializedOnceForAllFeatures) return;
>
> //authentication, user creation and other configuration initialization code
>
> intializeConfiguration():
>
> createUsers();
>
> isInitializedOnceForAllFeatures= true;
>
> }
>
>
> Let me know if this approach is correct or if there is any better design
> approach to handle this. Quick response is appreciated as I have to wrap
> this up quickly !! Also let me know if there is any good reference of
> things to keep in mind while designing automation tests using cucumber.
>
> II ) Another Issue I am getting is despite I refer a specific feature
> file in runner, it still invokes all feature file related step
> definitiaons and runs everthing. This is whats not expected. If I run a
> feature file , then it should ONLY run scenarios that are pertaining to
> that feature and NO other feature files should be picked. I am running
> via Eclipse. Right click on Runner class and run as Junit test.

Cucumber should run only the scenarios in the feature file you specify,
but will load all the step definition files.

>
> @RunWith(Cucumber.class)
>
> @CucumberOptions(features = "classpath:features/myTest.feature",
>
> plugin = {"pretty", "html:target/cucumber-html-report",
> "json:target/cucumber-report.json"})
>
> public class AppRunnerTest {
>
> }
>
>
> III) Obviously, across multiple feature files there ought to be same
> step definitions being used. How do we handle such cases? As there is no
> inheritance possible I cant have a common base class defining those step
> defintions. As order is not guaranteed its also not clear where to keep
> definitions ?

Step definitions are global. The meaning of "When I foo the bar" should
not change from scenario to scenario.

> IV) Sometimes a step definition that we use in a scenario can be put in
> as @Given but in some other scenario that step definition falls under
> @When or @And. How do we handle such cases. There seems to be a high
> need for me while writing automated tests. Same step definition method
> cannot have multiple tags also like @Given, @And

Cucumber doesn't really care whether you use @Given or @When. That's for
human communication. The step definition will still run, even if it's
introduced differently in the scenario.
- George

Anna

unread,
Dec 14, 2015, 7:10:46 AM12/14/15
to Cukes
Thanks. See my response below:


On Monday, December 14, 2015 at 5:16:37 PM UTC+5:30, George Dinwiddie wrote:
Anna,

On 12/14/15 3:03 AM, Anna wrote:
> Hey George.
>
> Thanks for response.
> You hit it right.. we are using Cucumber to write automation tests.

Who reads your cucumber scenarios?

Anna  - Well the scenarios are provided/read/reviewed by PM/Dev so idea is that requirements can be easily provided in human readable format. 

You might find
https://cucumber.io/blog/2014/03/03/the-worlds-most-misunderstood-collaboration-tool
helpful.

> The
> project has multiple feature files and as part of any automation
> process, we need some initialization and tear down code which needs to
> be run once, before any feature/scenarios run.
>
> Initially I put the initialization code in each of my test class (which
> has its feature file) under @Before method . However I realized that
> when you run the feature file through Runner it executes all feature
> file present in the features folder (despite me providing a specific
> feature file in the annotation like below which is another
> issue/bottleneck).
> and so initialization code gets run multiple times per feature/test class.

Cucumber will run only the feature file you specify, but it will load
all the step definitions. Step definitions do not match feature files.
All of your @Before methods will get run before each scenario.

See http://zsoltfabok.com/blog/2012/09/cucumber-jvm-hooks/ for more info
on how hooks work.

Anna-  Thanks George. I did look at http://zsoltfabok.com/blog/2012/09/cucumber-jvm-hooks/ this , probably I am actually following kind of similar approach by putting common setup code in a Setup class with static fields. Each test class then encapsulates the Setup class and calls the initialize method from @Before annotation. 
However, I was thinking if we could have some tags at feature level, this could have saved lot of effort and would have been cleaner.  
Anna - I think concern is ONLY wherein you do not want to run common data seed/setup code with each test class. Loading step definitions is fine.  What I have done is I have put in the common step definitions in Setup class (for ex, user logs in)  and because the Setup class is encapsulated in each of the test class, i can easily refer it in test class.
>
> @RunWith(Cucumber.class)
>
> @CucumberOptions(features = "classpath:features/myTest.feature",
>
> plugin = {"pretty", "html:target/cucumber-html-report",
> "json:target/cucumber-report.json"})
>
> public class AppRunnerTest {
>
> }
>
>
> III) Obviously, across multiple feature files there ought to be same
> step definitions being used. How do we handle such cases? As there is no
> inheritance possible I cant have a common base class defining those step
> defintions. As order is not guaranteed its also not clear where to keep
> definitions ?

Step definitions are global. The meaning of "When I foo the bar" should
not change from scenario to scenario.

> IV) Sometimes a step definition that we use in a scenario can be put in
> as @Given but in some other scenario that step definition falls under
> @When or @And. How do we handle such cases. There seems to be a high
> need for me while writing automated tests. Same step definition method
> cannot have multiple tags also like @Given, @And

Cucumber doesn't really care whether you use @Given or @When. That's for
human communication. The step definition will still run, even if it's
introduced differently in the scenario.

Anna - That sounds good. So this means I can have same step definition with different tags ? I did try this out but it gave me issues. I will check that again and will let u know in case it doesnt work for me

Roberto Lo Giacco

unread,
Dec 15, 2015, 8:25:50 AM12/15/15
to Cukes


Il giorno lunedì 14 dicembre 2015 13:10:46 UTC+1, Anna ha scritto:
Thanks. See my response below:

On Monday, December 14, 2015 at 5:16:37 PM UTC+5:30, George Dinwiddie wrote:
Anna,

On 12/14/15 3:03 AM, Anna wrote:
> Hey George.
>
> Thanks for response.
> You hit it right.. we are using Cucumber to write automation tests.

Who reads your cucumber scenarios?

Anna  - Well the scenarios are provided/read/reviewed by PM/Dev so idea is that requirements can be easily provided in human readable format. 

You might find
https://cucumber.io/blog/2014/03/03/the-worlds-most-misunderstood-collaboration-tool
helpful.

I believe you should really spend a few minutes reading this blog post a couple of times: it will be illuminating to highlight where and what you are misusing. That's a lot more important than the hooks link, trust us.
Cucumber is not a test automation tool and using it as such is a waste of time for your developers. It took me quite a while to understand the difference, but once I did it was illuminating and I started to really get the benefit of cucumber and gherkin. 

Priyam Ghosh

unread,
Nov 1, 2017, 9:30:18 AM11/1/17
to Cukes
Hi Shivi - I know it is quite a long you have posted it, but I would like to know if you resolved the problem stated above? I'm facing the same issue here.

Chuck van der Linden

unread,
Nov 2, 2017, 12:57:52 PM11/2/17
to Cukes


On Wednesday, November 1, 2017 at 6:30:18 AM UTC-7, Priyam Ghosh wrote:
Hi Shivi - I know it is quite a long you have posted it, but I would like to know if you resolved the problem stated above? I'm facing the same issue here.

1) please don't play necromancer and raise two year old threads from the dead.

2) the same advice given to Anna in the thread above would be the first thing I would recommend to you.

3) the 'problem' if there is one is a misunderstanding of how to use the tool properly, not an issue with the tool, it is resolved via educating yourself, not with some magic code trick.   
Message has been deleted

Andrew Premdas

unread,
Jan 15, 2018, 5:34:26 PM1/15/18
to cu...@googlegroups.com


On 15 January 2018 at 21:44, Drew <drew.m...@gmail.com> wrote:
And this folks, is why relatively new folks feel discouraged from asking questions...


I really don't think this helps at all Drew. All you are doing here is discouraging people from making sensible polite replies to poorly constructed questions. The alternative is that nobody replies to Priyam's post which surely is worse.

All best

Andrew

--
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+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



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

Roberto Lo Giacco

unread,
Jan 15, 2018, 5:36:18 PM1/15/18
to cu...@googlegroups.com
On Mon, Jan 15, 2018 at 10:44 PM, Drew <drew.m...@gmail.com> wrote:
And this folks, is why relatively new folks feel discouraged from asking questions...

​Hi Drew,

Why do you say so? I mean, Priyam Ghosh was not even asking a question​, he was rather asking for a code snippet or some clever solution on something advised as a tool misuse... And at the same time he was reviving an old thread, which is against forum rules.

In my opinion this is like watching someone entering into a public office, asking if there is way to jump the line while smoking a sigarette at the same time: wouldn't you say "hey man, line up for the queue and please, stop smoking, it's against the rules!"? To me the answer from 
Chuck van der Linden 
sounds like a nice warning... may be I am biased, but I don't see a reason why other people with question and seeking for advice shouldn't be posting their requests. But if a request has already received an answer and you were so good to find the answer, why should you ask for a solution which has been advised as a misuse? Doesn't it sound like asking if there's a way to skip the line?

May be I'm missing something...

Regards,
   Roberto

Drew

unread,
Jan 15, 2018, 6:32:31 PM1/15/18
to Cukes
I deleted that post. Not sure why it shows? I get what you're saying and after reading my original post, I deleted...

Roberto Lo Giacco

unread,
Jan 15, 2018, 7:00:09 PM1/15/18
to cu...@googlegroups.com
Il giorno mar 16 gen 2018 alle 00:32 Drew <drew.m...@gmail.com> ha scritto:
I deleted that post. Not sure why it shows? I get what you're saying and after reading my original post, I deleted...

I'm happy we share a common view, but please keep helping us improve: I personally welcome any critique, especially the constructive ones :-)

Marit van Dijk

unread,
Jan 18, 2018, 3:16:08 AM1/18/18
to Cukes
Hi Priyam,

The problem Shivi is (was) experiencing is that the step definitions appear to be tied to specific feature files. As @Aslak said, this is not the way to go. Step definitions are intended to be used by *any* feature file; that way you only have to define them once. If you define the same step multiple times, Cucumber will see it as a duplicate.
In fact, feature tied steps are an anti-pattern, as described here: https://github.com/cucumber/cucumber/wiki/Feature-Coupled-Step-Definitions-(Antipattern)

So if both features are doing the same thing for the same step (in the example, `Given user visiting the newsroom page`) you will only need to define it once.
If both features are intended to do something *else* for this step, you should rename them and define them differently.

Hope this helps,
Regards, Marit
Reply all
Reply to author
Forward
0 new messages