run Cucumber tags in a particular order through Maven (in Eclipse or command line)

1,763 views
Skip to first unread message

Styris

unread,
Nov 20, 2015, 6:41:09 PM11/20/15
to Cukes
I have two separate feature files (existingUser.feature and inbox.feature). the existingUser.feature has a tag that is @logoutNewUser. The inbox.feature has a @inbox tag. I need to run @inbox tag first..and then @logoutNewUser. I have tags specified for both in my Cucumber runner class in that order but however it runs the @logoutNewUser first. My question is twofold:
1. How can I specify the tags to run in the order that I mentioned
2. Do I need to specify the tags in the POM.xml instead (in that case the feature and the glue in POM.xml as well) ?
3. Or is my best solution to name the feature files in an order so that it is executed accordingly (cheap solution but may work) ?

Thanks in advance!!

Andrew Premdas

unread,
Nov 21, 2015, 1:16:23 AM11/21/15
to cu...@googlegroups.com
You shoud not be using tags to do this sort of functionality.

If a scenario requires that a user should log out, then it should say that. A cavaet for this situation is that we can assume a new user is logged out when the scenario starts, as each scenario starts with a clean slate e.g.

Scenario: User logs out
  Given I am logged in
  When I log out
  Then I should ...

Scneario: User visits site
  When I visit the site
  Then I should be logged out # you are logged when the scenario starts

Lets explore something more complicated. Say we have an ecommerce site which sells stationery too companies. One thing we would like is to allow users to reorder a previously used order. We could write something like

Scenario: Reordering
  Given I have registered
  And I have a completed order
  And I am not logged in
  And I h ...

but this is tedious and repetitive.

We could try and use tags for this, but that would be a mistake. Instead we can just use a normal scenario using language with a higher level of abstraction. For this we need a name, which we have already: 'reordering', so we can write

Scenario: Reordering
  Given I am reordering
  When I reorder
  Then my items should be added to the basket
  And I should be taken to the checkout

To do this we push the detail down into the step defintion e.g

Given "I am reordering" do
   reorder_for @i
end

and implement this with something like

module ReorderSH
  def reorder(user, order = nil)
     order ||= default_order
     register user
     create_existing_order user: user, order: order
     ...
   end
end
World ReorderSH
   

Notice how the helper is doing all the work that you want to use tags to do.

Tags really should only be used to effect features are run. The canonical example being the @wip (work in progress).

If you need to control the order in which tags are run then you are misusing tags. If you provide the scenarios you have written that use tags in this particular way, we can provide you with better/simpler ways to write and implement them.

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



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

Styris

unread,
Nov 30, 2015, 6:22:02 PM11/30/15
to Cukes

Thanks Andrew. I am writing my tests in JAVA, and I still have a few questions (please pardon my Cucumber knowledge-level).

1. Say I have a feature file (named as exitingUserLogin.feature) that has two scenarios each for login and login (both are tagged as @login & @logout respectively). I now have another feature file (inbox0.feature) that has a scenario that asks the User to login and go to the Settings screen and then log out. in this case I basically need to reuse the @login and @login from the other feature file in order to run my scenarios. If I understand correct you are suggesting that this is where I need to use helper method(s), correct? I am not sure what a helper method is or how to use that. So any way you can provide help with that?


2. I still would end up with a bunch of feature files containing all the scenarios. Obviously they will be run based on the Alphabetical order. But what if I wanted to run them in a particular order. Is that even possible?


If you could please…I would appreciate if you could provide the answers in that order (1. & 2.).



Thanks in advance!

Andrew Premdas

unread,
Dec 2, 2015, 5:40:56 AM12/2/15
to cu...@googlegroups.com
I can't help you with the 1. as I don't use Java. In ruby you can define methods in a module which you then add to Cucumber's World object. This has the effect of adding your methods to the scope of the step definitions. So I can have

module SignInStepHelper
  def sign_in_as(user)
    # do some stuff
  end
end
World SignInStepHelper

When "I sign in" do
  sign_in_as @i
end

Now I can use this sign_in__as method in any other step definition e.g.

Given "I have signed in" do
  sign_in_as @I
end

We can even support people using different language e.g. login instead of sign_in, whilst still keeping our code DRY.

Given "I am logged in as a newly registered user" do
   @i = create_newly_registered_user
   sign_in_as @i
end

etc. etc.

So you can talk about sign_in, in as many ways as you want. It doesn't matter if you have 5 different ways of expressing how you sign in, and 5 different step definitions, because they can all make the same call.

2. Scenarios are independent and can and should be run in a completely random order. You should never tie one scenario to another. Each scenario starts with a clean slate. The same applies to feature files. If you want to build on the functionality of previous scenarios you should extract methods and call those methods to get your functionality.

HTH


Styris

unread,
Dec 2, 2015, 9:29:45 PM12/2/15
to Cukes
Thanks Andrew I can certainly use those tips.

Can anyone else, please, help or provide tips with my question below on how I can use helper methods in JAVA?
In almost all of the cases I am having to reuse the @login and @logout scenarios (both of which are in a particular feature file) for a lot of other scenarios. Each time I end up reusing the same steps in the feature file but is there a better way, like a helper method, that someone can suggest?


Thanks
Reply all
Reply to author
Forward
0 new messages