Rspec测试,关于顺序和before(:all)

发布于 2024-12-12 04:54:43 字数 1257 浏览 0 评论 0原文

如果我有如下的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)< /code> 声明实际上在每个子上下文中被调用?我以为它在整个测试过程中只调用一次,但是当我运行测试时,我经历的是 before(:all) 中的代码 get在每个 上下文中执行,那就是每个上下文启动时都会运行,为什么?

(正如您所注意到的,我的 before(:all) 代码中有“puts”,并且在运行test,为什么? before(:all) 不应该在整个测试期间只执行一次??)

2. 当我运行测试时,为什么测试运行的顺序是从底部上下文到顶部上下文(在每个​​上下文中,“it”的顺序是从上到下)?那么如何在上下文级别更改测试顺序呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一张白纸 2024-12-19 04:54:43

1) before(:all) 应该只运行一次...但是有一个已知的问题,它完全按照您指出的方式进行。这里讨论:
http://rubyforge.org/pipermail/rspec-users/2010-September/018

2) 测试必须彼此独立运行。以确保您没有做出任何假设。因此,测试套件通常以随机或相反的顺序运行测试 - 以确保您不会这样做。

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.

任谁 2024-12-19 04:54:43

首先,阅读 this - 其次是页面底部的部分

,这是我最近学到的一个技巧是你可以使用 after(:all)

after :all do
  @users.each { |user| user.destroy! }
end

非常简洁

Firstly, read this - especially the part at the bottom of the page

secondly, a trick I learned recently is that you can use after(:all)

after :all do
  @users.each { |user| user.destroy! }
end

Pretty neat

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文