[rspec-users] Before and After blocks for individual feature files?

Julian Leviston julian at leviston.net
Tue Apr 28 21:51:20 EDT 2009


Hey Ben,

It'd be kinda cool if there was a sort of before and after for a  
feature rather than each scenario. Is there?

(Rails context) We often need this. It'd be really helpful for things  
like when we want to test about 15 things on a particular web page,  
and they don't require fresh data. We end up with a login and setup  
type background which gets run every time rather than simply once.

I guess we could refactor it into a set of examples perhaps... would  
that work? It just strikes me as quite complicated. It'd be awesome if  
we had sub-scenarios (and they could be specified to levels) ;-)  
Perhaps I'm just being too complicated.

I loved your rubyconf talk presentation, BTW. We kinda took exception  
to the bit where you said "Selenium just works", though. There are a  
number of things where the connection between selenium and webrat is a  
little tenuous and finicky about.

Also we seem to be having timing issues for AJAX requests with  
Selenium. Webrat doesn't seem to want to wait until the AJAX request  
as finished before doing the next thing. Any ideas here?

Julian.

On 29/04/2009, at 4:28 AM, Ben Mabey wrote:

> Arco wrote:
>> I'd like to do this:
>>
>> Feature: user signup
>> Before:
>>   Given I have a cleaned up database
>> Scenario Outline: Sign Up
>>   Given I am on the signup page
>>   When I sign up using <userid>
>>   Then I should see <message>
>>  Examples:
>>   |userid     |message           |
>>   |userX      |successful signup |
>>   |userX      |duplicate userid  |
>>
>> "I have a cleaned up database" runs before every example, making the
>> second example ('duplicate userid') fail.
>>
>
>
> You could use Background and it would work just like you want it to:
>
> Feature: user signup
> Background:
>  Given I have a cleaned up database
> Scenario Outline: Sign Up
>  Given I am on the signup page
>  When I sign up using <userid>
>  Then I should see <message>
> Examples:
>  |userid     |message           |
>  |userX      |successful signup |
>  |userX      |duplicate userid  |
>
>
> However, I would not encourage this.  You should try to avoid using  
> technical words, such as database, in your features.  If anything  
> you could say "Given no users exist" or something like that.   
> Keeping your database clean is something you generally want for  
> every scenario though.  So I would suggest putting the code in your  
> "Given I have a cleaned up database" code into a Before block.  The  
> wiki has a page on using the Before hook: http://wiki.github.com/aslakhellesoy/cucumber/hooks
>
> Basically, in your env.rb file you will add something like:
>
> Before do
> Database.clean! # or however you clean your DB
> end
>
>
>
> HTH,
> Ben
>
>
>> On Apr 28, 9:38 am, aslak hellesoy <aslak.helle... at gmail.com> wrote:
>>
>>> On Tue, Apr 28, 2009 at 6:15 PM, Arco <akl... at gmail.com> wrote:
>>>
>>>> OK - I found a workaround.  I simply tag the first scenario with
>>>> '@first', then
>>>> do Before('@first') and i get what I want - executing a block  
>>>> once for
>>>> the feature file.
>>>>     Except for one problem:  most of my scenarios are done as  
>>>> scenario
>>>> outlines, which
>>>> are run multiple times - once for each row of my Example table.
>>>>     A workaround to that problem might be to put a 'dummy'  
>>>> scenario that
>>>> is run before the other scenarios in my feature file...
>>>>      @first
>>>> Scenario: Call a before block before running other scenarios...
>>>>     But this puts junk in my feature files.  Is there a better,  
>>>> cleaner
>>>> way??
>>>>
>>> a) Why do you need one thing to happen before a feature?
>>> b) Why can't you do it before each scenario?
>>>
>>> Aslak
>>>
>>>
>>>
>>>
>>>> On Apr 28, 8:32 am, Arco <akl... at gmail.com> wrote:
>>>>
>>>>> I also would like a hook that executes a block once before  
>>>>> running a
>>>>> feature file.
>>>>>       In my testing i found that:
>>>>> - Background: executes before each scenario
>>>>> - Before executes before each scenario
>>>>> - Before('@tag') executes before each scenario
>>>>>       Is there a way to execute a block once before each  
>>>>> feature, but not
>>>>> before each scenario?
>>>>>       On Apr 28, 7:08 am, aslak hellesoy  
>>>>> <aslak.helle... at gmail.com> wrote:
>>>>>
>>>>>>> Hi -- is it possible to set before and after blocks for  
>>>>>>> individual
>>>>>>>
>>>> feature
>>>>
>>>>>>> files?
>>>>>>>
>>>>>> Yes. Use tagged hooks:
>>>>>>
>>>> http://wiki.github.com/aslakhellesoy/cucumber/hooks
>>>>
>>>>>> Aslak
>>>>>>
>>>>>>> I've tried putting them in step files, but they just get called
>>>>>>>
>>>> before
>>>>
>>>>>>> everything, like they'd been declared in env.rb, which is  
>>>>>>> consistent
>>>>>>>
>>>> with
>>>>
>>>>>>> how I thought cucumber worked, but I thought I'd best try it  
>>>>>>> anyway.
>>>>>>> Anyway, I have some features that require a specific state be  
>>>>>>> set up
>>>>>>>
>>>> before
>>>>
>>>>>>> they run -- is this possible to do, and how would I go about  
>>>>>>> doing
>>>>>>>
>>>> it?
>>>>
>>>>>>> Thanks for any & all help,
>>>>>>>  Doug.
>>>>>>>           _______________________________________________
>>>>>>> rspec-users mailing list
>>>>>>> rspec-us... at rubyforge.org
>>>>>>> http://rubyforge.org/mailman/listinfo/rspec-users
>>>>>>>
>>>>>> _______________________________________________
>>>>>> rspec-users mailing list
>>>>>> rspec-us... at rubyforge.orghttp://
>>>>>>
>>>> rubyforge.org/mailman/listinfo/rspec-users
>>>>
>>>>> _______________________________________________
>>>>> rspec-users mailing list
>>>>> rspec-us... at rubyforge.orghttp://
>>>>>
>>>> rubyforge.org/mailman/listinfo/rspec-users
>>>> _______________________________________________
>>>> rspec-users mailing list
>>>> rspec-us... at rubyforge.org
>>>> http://rubyforge.org/mailman/listinfo/rspec-users
>>>>
>>>
>>> _______________________________________________
>>> rspec-users mailing list
>>> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
>>>
>> _______________________________________________
>> rspec-users mailing list
>> rspec-users at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/rspec-users
>>
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list