58

The before and after hook documentation on Relish only shows that before(:suite) is called prior to before(:all).

When should I use one over the other?

Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398

2 Answers2

103

When a before(:all) is defined in the RSpec.configure block it is called before each top level example group, while a before(:suite) code block is only called once.

Here's an example:

RSpec.configure do |config|
  config.before(:all) { puts 'Before :all' }
  config.after(:all) { puts 'After :all' }
  config.before(:suite) { puts 'Before :suite' }
  config.after(:suite) { puts 'After :suite' }
end

describe 'spec1' do
  example 'spec1' do
    puts 'spec1'
  end
end

describe 'spec2' do
  example 'spec2' do
    puts 'spec2'
  end
end

Output:

Before :suite
Before :all
spec1
After :all
Before :all
spec2
After :all
After :suite
Matilda Smeds
  • 1,384
  • 11
  • 18
Leo
  • 2,088
  • 1
  • 15
  • 14
  • 1
    my question is: "If I load some seed data like admin User using `before suite` wont that data be cleared after example is run?" – Shiva Feb 15 '16 at 03:06
  • No they will stay, you have to deal with them manually. Only before(:example) is inside transaction. – Foton Sep 21 '16 at 13:04
  • :suite and :context (= :all) are outside transaction. No matter how deep are contexts nested. – Foton Sep 21 '16 at 13:06
0

You can also use before(:suite) to run a block of code before any example groups are run. This should be declared in RSpec.configure

http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/Hooks

denis.peplin
  • 9,585
  • 3
  • 48
  • 55
  • I'm observing how a guy that was working at my position before me, misunderstood the "This should be declared in `RSpec.configure`" and has put all his pre-run fixtures into `RSpec.configure` -- they now run even with `--dry-run`. I'm told "to not touch anything! only add new tests"... – Nakilon Aug 20 '18 at 13:38
  • This was 2013, the answer is probably irrelevant anyway. – denis.peplin Aug 20 '18 at 18:31