If I have rspec test like following:
context "main context" do
before(:all) do
# code for before :all
puts "my fore :all"
end
describe "Scenario-1" do
context "my context 1" do
it "should blablabla" do
end
it "should blablabla" do
end
end
context "my context 2" do
it "should blablabla" do
end
it "should blablabla" do
end
end
end # end of describe "Scenario-1"
describe "Scenario-2"
context "my context 3" do
it "should blablabla" do
end
it "should blablabla" do
end
end
end #end of "Scenario-2"
end #end of main context
Two questions to ask:
1. Is it so that the before(:all) declare actually get called in each sub context ? I thought it is only called once during the whole test, but when I run my test, What I experienced is that the code in before(:all) get executed in each context, that’s it get run when each context started, why?
(As you noticed I have “puts” as part of my before(:all) code, and I saw this puts in each sub-context when run the test, why? isn’t before(:all) should only be executed once during the whole test??)
2. When I run my test, Why the order of the test running is from bottom context to up context (while inside each context, the order of “it” is up-down)? How to change the test order on context level then?
1) before(:all) should be running only once… but there is a known isue about it doing exactly what you’ve pointed out. Discussed here:
http://rubyforge.org/pipermail/rspec-users/2010-September/018
2) tests must run independently of one another. to ensure that you aren’t making any assumptions. Thus testing suites often run the tests in random or reverse order – to be sure that you aren’t doing that.