【问题标题】:How to run tests in order they are written?如何按编写顺序运行测试?
【发布时间】:2013-04-26 13:11:04
【问题描述】:

我正在尝试编写测试狮身人面像搜索的测试。我需要用户将一些参数传递给 api,并基于该参数 shpinx 来执行搜索。

我有以下 3 个测试

在 test_helper.rb 中我已经设置好了一切

  require 'factory_girl'
  require 'thinking_sphinx/test'
  ThinkingSphinx::Test.init
  .........
  class ActiveSupport::TestCase

  self.use_transactional_fixtures = false
  self.use_instantiated_fixtures  = false
  fixtures :all

还有我的测试

test "should 1st test" do
  ThinkingSphinx::Test.start
  # uthenticatoin and creating records in databese with Factory Girl
  ThinkingSphinx::Test.index

  get "some/path", {params}, @headers

  assert_response :success
end

test "should 2nd test" do
  # uthenticatoin and creating records in databese with Factory Girl
  ThinkingSphinx::Test.index

  get "some/path", {params}, @headers

  assert_response :success
  # other assertions
end

test "should 3rd test" do
  # uthenticatoin and creating records in databese with Factory Girl
  ThinkingSphinx::Test.index

  get "some/path", {params}, @headers

  assert_response :success
  # other assertions
  ThinkingSphinx::Test.stop
end

我不知道为什么我的测试不是按照编写的顺序运行,而是按照第 2、第 3、第 1 次运行 我怎样才能使测试按照编写的顺序运行。我正在使用基本的 Rails Test::Unit。 由于测试细节,订单对我来说很重要。 谢谢。

【问题讨论】:

    标签: ruby-on-rails-4 factory-bot ruby-1.9.3 testunit


    【解决方案1】:

    您的测试绝不应该以顺序很重要的方式编写。我明白你为什么想要这个订单,并且有办法解决这个问题。试试这个:

    setup do
      ThinkingSphinx::Test.start
    end
    
    # Your tests
    
    teardown do
      ThinkingSphinx::Test.stop
    end
    

    这使得 each 测试之前 ThinkingSphinx::Test 开始,并在 each 测试之后停止。这是设置它的理想方式,因此现在运行测试的顺序无关紧要。

    但是,如果ThinkingSphinx::Test.start 是一个很长的过程,您可能不希望它在每个测试中都运行。我不知道TestUnit 是否让您能够在整个套件或一组测试之前运行设置,但在 RSpec 中您能够做到这一点,这将为您提供更好的服务。

    【讨论】:

      猜你喜欢
      • 2015-12-21
      • 2015-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-24
      • 2015-12-18
      相关资源
      最近更新 更多