如果我有如下rspec测试:
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
要问的两个问题:
1. before(:all)
声明实际上在每个子上下文 中被调用了吗?我以为它在整个测试期间只被调用一次,但是当我运行我的测试时,我经历的是 before(:all)
get在每个 context
中执行,这是在每个context
启动时运行,为什么?
(正如您所注意到的,我将“puts
”作为我的before(:all)
代码的一部分,并且我在每个子上下文中都看到了这个 puts当运行测试时,为什么?before(:all)
不应该在整个测试期间只执行一次吗??)
2. 当我运行我的测试时,为什么测试运行的顺序是从底部上下文到向上上下文(而在每个上下文中,“它的顺序“是上下)?那么如何在上下文级别更改测试顺序呢?
最佳答案
1) before(:all) 应该只运行一次......但是有一个已知的问题是它完全按照你所指出的去做。在这里讨论: http://rubyforge.org/pipermail/rspec-users/2010-September/018
2) 测试必须相互独立运行。以确保您没有做出任何假设。因此,测试套件通常以随机或相反的顺序运行测试 - 以确保您没有这样做。
https://stackoverflow.com/questions/7887328/