# \[Rspec\] How do you nest before(:all) or after(:all) blocks?

**URL:** <https://www.ruby-forum.com/t/rspec-how-do-you-nest-before-all-or-after-all-blocks/159407>\
**Category:** RSpec\
**Created:** [February 4, 2009, 10:11am UTC](https://www.ruby-forum.com/t/rspec-how-do-you-nest-before-all-or-after-all-blocks/159407 "2009-02-04T10:11:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [February 4, 2009, 10:11am UTC](https://www.ruby-forum.com/t/rspec-how-do-you-nest-before-all-or-after-all-blocks/159407/1 "2009-02-04T10:11:49Z")

</div>

Hi all,

I’m new to Rspec but loving it so far and looking to use it as a  
replacement for a Test::Unit framework I have which drives a web app  
via Watir. So far, things have worked very well with Rspec but I can’t  
get my head around how before/after(:all) blocks would work in nested  
groups, or even if what I am doing is possible.

What I want to do is something like this:

describe "This is a set of tests: " do  
before(:all) do  
puts “this should only appear once”  
end

describe “Test 1” do  
puts “test 1”  
end

describe “Test 2” do  
puts “test 2”  
end  
end

Expecting the before(:all) block to run only once and see something  
like:

- this should only appear once
- test 1
- test 2

but what I am getting is:

- this should only appear once
- test 1
- this should only appear once
- test 2

So my question is, how can you add before/after(:all) blocks that run  
once and only once for for all examples, if all the examples are  
nested in one main describe block? Is that something that’s even  
possible? I’d appreciate any pointers anyone may have. Thanks!

Regards,

John

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [February 4, 2009, 3:09pm UTC](https://www.ruby-forum.com/t/rspec-how-do-you-nest-before-all-or-after-all-blocks/159407/2 "2009-02-04T15:09:59Z")

</div>

On Wed, Feb 4, 2009 at 3:07 AM, John K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> describe "This is a set of tests: " do  
> end
> 
> - this should only appear once
> - test 1
> - this should only appear once
> - test 2
> 
> So my question is, how can you add before/after(:all) blocks that run  
> once and only once for for all examples, if all the examples are  
> nested in one main describe block? Is that something that’s even  
> possible? I’d appreciate any pointers anyone may have. Thanks!

Right now it’s not possible. There’s an open ticket on this:

[http://rspec.lighthouseapp.com/projects/5645/tickets/632](http://rspec.lighthouseapp.com/projects/5645/tickets/632)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [February 4, 2009, 4:33pm UTC](https://www.ruby-forum.com/t/rspec-how-do-you-nest-before-all-or-after-all-blocks/159407/3 "2009-02-04T16:33:22Z")

</div>

John K. wrote:

> describe "This is a set of tests: " do  
> end

Why do you want before(:all)? Generally it’s use is disregarded, just  
as global variables are considered harmful, not because they can’t be  
used well, but because 99% of the times they aren’t.

Scott

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [February 4, 2009, 4:46pm UTC](https://www.ruby-forum.com/t/rspec-how-do-you-nest-before-all-or-after-all-blocks/159407/4 "2009-02-04T16:46:54Z")

</div>

Thanks for the reply David - much appreciated. At least now I know  
that it can’t be done, rather  
than pursuing it further for now. I’ll just have to live without that  
functionality for now and add  
it later on if a fix is made available.

Regards,

John

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [February 4, 2009, 7:23pm UTC](https://www.ruby-forum.com/t/rspec-how-do-you-nest-before-all-or-after-all-blocks/159407/5 "2009-02-04T19:23:53Z")

</div>

> Why do you want before(:all)? Generally it’s use is disregarded, just  
> as global variables are considered harmful, not because they can’t be  
> used well, but because 99% of the times they aren’t.

Because I want to be able to create a single @browser object at the  
beginning  
of my tests and have it passed to all examples. Without nested  
examples, that  
works just fine - I can create my browser instance in the before(:all)  
block and  
avoid using a global variable. In nested examples, I don’t see a good  
way to  
do that - I could always set up the browser instance as a global (did  
that in  
the past with Test::Unit and many Watir users do so) but I’d like to  
avoid this.

If I didn’t use a before(:all) block or a global variable, would there  
be any way to  
pass my browser object to all example groups?

Currently my tests look like this:

describe “A series of tests” do  
before(:all) do  
@browser = Watir::Browser.new  
end

it “Test 1: it should go to [www.google.com](http://www.google.com)” do  
@browser.goto(“[www.google.com](http://www.google.com)”)  
end  
end

Obviously, been able to use nested describe groups would make the  
tests easier  
to read for non-coders here and that’s why I wanted to use them…

Regards,

John

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [February 5, 2009, 10:22am UTC](https://www.ruby-forum.com/t/rspec-how-do-you-nest-before-all-or-after-all-blocks/159407/6 "2009-02-05T10:22:35Z")

</div>

> Have you considered using Cucumber rather than RSpec as the driver to  
> run these tests?

TBH, I haven’t really looked at Cucumber much, as I’m just getting  
started with Rspec itself. My impression though was that Cucumber  
replaced the story runner and the way my tests are structured just  
seems better suited to Rspec, rather than written as user stories…

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [October 24, 2013, 8:39pm UTC](https://www.ruby-forum.com/t/rspec-how-do-you-nest-before-all-or-after-all-blocks/159407/7 "2013-10-24T20:39:03Z")

</div>

David C. wrote in post #777566:

> On Wed, Feb 4, 2009 at 3:07 AM, John K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > describe "This is a set of tests: " do  
> > end
> > 
> > - this should only appear once
> > - test 1
> > - this should only appear once
> > - test 2
> > 
> > So my question is, how can you add before/after(:all) blocks that run  
> > once and only once for for all examples, if all the examples are  
> > nested in one main describe block? Is that something that’s even  
> > possible? I’d appreciate any pointers anyone may have. Thanks!
> 
> Right now it’s not possible. There’s an open ticket on this:
> 
> [Lighthouse - Beautifully Simple Issue Tracking](http://rspec.lighthouseapp.com/projects/5645/tickets/632)

I could not see the status of the ticket as it was restricted…Any  
updates with regards to the use of the global variable objects in the  
same script call before test… Any best practices mentioned or  
suggested???

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [February 5, 2009, 8:56am UTC](https://www.ruby-forum.com/t/rspec-how-do-you-nest-before-all-or-after-all-blocks/159407/8 "2009-02-05T08:56:23Z")

</div>

On 4 Feb 2009, at 17:55, John K. wrote:

> > Why do you want before(:all)? Generally it’s use is disregarded,  
> > just  
> > as global variables are considered harmful, not because they can’t be  
> > used well, but because 99% of the times they aren’t.
> 
> Because I want to be able to create a single @browser object at the  
> beginning  
> of my tests and have it passed to all examples. Without nested  
> examples, that

Have you considered using Cucumber rather than RSpec as the driver to  
run these tests?

Matt W.  
[http://blog.mattwynne.net](http://blog.mattwynne.net)

> **[Find the Best Concerts Near You, Tour Dates & Tickets Worldwide | Songkick](https://www.songkick.com/)**
>
> Find live music near you, track your favorite artists, get instant concert alerts and buy tickets for all the best upcoming concerts.

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [October 24, 2013, 8:39pm UTC](https://www.ruby-forum.com/t/rspec-how-do-you-nest-before-all-or-after-all-blocks/159407/9 "2013-10-24T20:39:56Z")

</div>

> [Lighthouse - Beautifully Simple Issue Tracking](http://rspec.lighthouseapp.com/projects/5645/tickets/632)

something i wish i could get additional information from other users??

I could not see the status of the ticket as it was restricted…Any  
updates with regards to the use of the global variable objects in the  
same script call before test… Any best practices mentioned or  
suggested???
