Passing arrays in tables

5,663 views
Skip to first unread message

mswinson

unread,
Feb 13, 2012, 3:04:31 PM2/13/12
to Cukes
Hi,

I have a scenario outline I'm trying to write where I want to pass an
array
as one of the parameters as follows -

Scenario Outline:
When I search for all <search_terms>
Then I should get a list of recipes
And the list of recipes should contain a recipe with the title
<recipe_title>

Examples:
| search_terms | recipe_title |
| chocolate AND 'Nigella Lawson' | "White chocolate and mint mousse"
|


mswinson

unread,
Feb 13, 2012, 3:13:47 PM2/13/12
to Cukes
Hi,

I have a scenario outline I'm trying to write where I want to pass an
array
as one of the parameters as follows -

Scenario Outline:
When I search for all <search_terms>
Then I should get a list of recipes
And the list of recipes should contain a recipe with the title
<recipe_title>

Examples:
| search_terms | recipe_title |
| "[ 'chocolate' , 'Nigella Lawson']" | "White chocolate and mint
mousse" |


but cucumber does'nt seem to parse it correctly ( or rather, as I
want ).
My knowledge of ruby is not that deep, so it maybe that I might be
missing
something to do with the way parameters are parsed.

1. Is there something I am missing re: the above example?
2. Is there way of rewriting the scenario to cope with params of
undefined
length so that I don't have to do the above?


I did think about the following, using the first two clauses to build
the query and issuing the request in the Then step. But that strikes
me as a bit clunky.

When I search for <searchterm>
And <searchterm2>
Then...


Thanks


mark

Andy Waite

unread,
Feb 13, 2012, 5:24:49 PM2/13/12
to Cukes
I would advise against using array syntax in your scenarios, as it
makes them hard to read.

A simple alternative would be to use a comma separated list surrounded
by quotes:

When I search for all <search_terms>
...
Examples:
| search_terms |
| chocolate, Nigella Lawson |

Then in your step definition you can call arg.split(', ') to get an
array of the terms.

Andy

Andy Waite

unread,
Feb 13, 2012, 5:28:14 PM2/13/12
to Cukes
Oops, that should be:

When I search for all "<search_terms>"


--
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 http://groups.google.com/group/cukes?hl=en.


Matt Wynne

unread,
Feb 16, 2012, 5:09:59 AM2/16/12
to cu...@googlegroups.com
+1

i would also consider getting rid of the scenario outline and just having a separate scenario for each set of search terms. Then you can express each list in a step data table:

When I search for all these terms:
  | chocolate          |
  | Nigella Lawson |
Then I should get a list of recipes
…


cheers,
Matt

--
Freelance programmer & coach
Author, http://pragprog.com/book/hwcuc/the-cucumber-book
Founder, http://www.relishapp.com/
Twitter, https://twitter.com/mattwynne


Andrew Premdas

unread,
Feb 17, 2012, 7:56:04 AM2/17/12
to cu...@googlegroups.com
|


--
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 http://groups.google.com/group/cukes?hl=en.


You should have a think about what you are trying to test here. There seems little point in trying to test that your search engine searches properly. I assume that you are using some tool to do your searching, that lots of other people use the tool and that the tool is tested by its authors. So time spent writing simplistic tests to ensure you get the 'correct' results is probably wasted time.

What you want to test is that you code uses the search engine correctly. In this case, you don't need to put the details of your search in the feature - they really aren't that relevant. So you could do something like

Given I have search results for foo
When I search for foo
Then I should see search results for foo

You could expand on this a little more e.g. add search results for bar - but again you want to ensure you are only (mostly?) testing what you are adding. Now you can stop writing complicated step definitions in cucumber that parse tables and arrays.

In general, if something is difficult to do in Cucumber stop and think about doing it a different way first of all

HTH

Andrew


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

mswinson

unread,
Feb 18, 2012, 10:44:29 AM2/18/12
to Cukes
Thanks for your reply.

I have to say that I should have been clearer about what I am testing.
Yes, I am using an existing tool which ultimately does the searching.
However what I am testing is a service which forwards a search request
to this tool, essentially it sits as a layer above it, and also
changes the way
in which the results are presented. Since this is specific to my
service, this
needs testing.

Regarding passing arrays into my step definitions, I managed to solve
it quite easily thanks to
Andy Waites suggestion above ( thanks ! ). Essentially it boiled down
to not having enough
understanding of the way Gherkin works.


Regards,

Mark







On Feb 17, 12:56 pm, Andrew Premdas <aprem...@gmail.com> wrote:

Andrew Premdas

unread,
Feb 18, 2012, 8:44:18 PM2/18/12
to cu...@googlegroups.com
On 18 February 2012 15:44, mswinson <mswi...@btinternet.com> wrote:
Thanks for your reply.

Your welcome
 
I have to say that I should have been clearer about what I am testing.
Yes, I am using an existing tool which ultimately does the searching.
However what I am testing is a service which forwards a search request
to this tool, essentially it sits as a layer above it, and also
changes the way
in which the results are presented. Since this is specific to my
service, this
needs testing.


Ok, but then the question you might one to ask is are you being clear in your feature that you are testing that specific functionality. I'm not saying that your current approach is wrong, just that there 'might' be quite alot of implied knowledge that could be expressed in the feature. In particular why does the business need this service, and why do the results need to be presented in a certain way.

The problem with using arrays in features in this sort of area is that you tend to specify what should happen, without specifying why it should happen. Aside from the problems of duplication in the feature, and the complexity of step definitions, you also have the issue that when the feature fails (in the future) not having the context from 'why'. can make fixing things alot harder.

Anyhow maybe something worth thinking about

All best

Andrew
Reply all
Reply to author
Forward
0 new messages