跳至内容 跳至搜索
方法
A
F
P
T
包含的模块

常量

Assertion = Minitest::Assertion
 

类公共方法

fixture_paths

返回 ActiveRecord::FixtureSet 集合。

在你的 test_helper.rb 中必须包含 require "rails/test_help"

# File activesupport/lib/active_support/test_case.rb, line 170
      

fixture_paths=(fixture_paths)

设置给定的路径到 fixture 集合。

也可以追加多个路径。

ActiveSupport::TestCase.fixture_paths << "component1/test/fixtures"

在你的 test_helper.rb 中必须包含 require "rails/test_help"

# File activesupport/lib/active_support/test_case.rb, line 176
    

parallel_worker_id()

如果测试正在并行运行,则返回当前的并行工作进程 ID,否则返回 nil。

ActiveSupport::TestCase.parallel_worker_id # => 2
# File activesupport/lib/active_support/test_case.rb, line 34
def parallel_worker_id
  @@parallel_worker_id
end

parallelize(workers: :number_of_processors, with: :processes, threshold: ActiveSupport.test_parallelization_threshold, parallelize_databases: ActiveSupport.parallelize_test_databases)

并行化测试套件。

接受一个 workers 参数,该参数控制进程被 fork 的次数。每个进程都会创建一个新的数据库,并以工作进程编号作为后缀。

test-database_0
test-database_1

如果设置了 ENV["PARALLEL_WORKERS"],则会忽略 workers 参数,并使用该环境变量。这对于 CI 环境或其他可能需要比本地测试更多工作进程的环境很有用。

如果工作进程数量设置为 1 或更少,则测试不会被并行化。

如果 workers 设置为 :number_of_processors,工作进程数量将设置为你所在机器的实际 CPU 核心数。

默认的并行化方法是 fork 进程。如果你想使用线程而不是进程,可以将 with: :threads 传递给 parallelize 方法。请注意,线程并行化不会创建多个数据库,并且不适用于系统测试。

parallelize(workers: :number_of_processors, with: :threads)

线程并行化直接使用 minitest 的并行执行器。进程并行化使用 Ruby DRb 服务器。

由于并行化会产生开销,因此只有当要运行的测试数量超过 threshold 参数时才会启用。默认值为 50,可以通过 config.active_support.test_parallelization_threshold 进行配置。

如果你想跳过 Rails 默认的每个进程创建一个数据库的机制,而是编写自己的实现,可以设置 parallelize_databases,或者通过 config.active_support.parallelize_test_databases 进行配置。

parallelize(workers: :number_of_processors, parallelize_databases: false)

请注意,如果你尝试仅使用一个数据库而使用多个进程,你的测试套件可能会死锁。

# File activesupport/lib/active_support/test_case.rb, line 107
def parallelize(workers: :number_of_processors, with: :processes, threshold: ActiveSupport.test_parallelization_threshold, parallelize_databases: ActiveSupport.parallelize_test_databases)
  case
  when ENV["PARALLEL_WORKERS"]
    workers = ENV["PARALLEL_WORKERS"].to_i
  when workers == :number_of_processors
    workers = (Concurrent.available_processor_count || Concurrent.processor_count).floor
  end

  if with == :processes
    ActiveSupport.parallelize_test_databases = parallelize_databases
  end

  Minitest.parallel_executor = ActiveSupport::Testing::ParallelizeExecutor.new(size: workers, with: with, threshold: threshold)
end

parallelize_before_fork(&block)

并行测试的 fork 前钩子。这可用于在进程 fork 之前运行任何内容。

在你的 test_helper.rb 中添加以下内容

class ActiveSupport::TestCase
  parallelize_before_fork do
    # run this before fork
  end
end
# File activesupport/lib/active_support/test_case.rb, line 132
def parallelize_before_fork(&block)
  ActiveSupport::Testing::Parallelization.before_fork_hook(&block)
end

parallelize_setup(&block)

并行测试的 setup 钩子。如果你有多个数据库或任何需要在进程 fork 后、测试运行前执行的行为,可以使用此钩子。

注意:此功能在线程并行化中不可用。

在你的 test_helper.rb 中添加以下内容

class ActiveSupport::TestCase
  parallelize_setup do
    # create databases
  end
end
# File activesupport/lib/active_support/test_case.rb, line 149
def parallelize_setup(&block)
  ActiveSupport::Testing::Parallelization.after_fork_hook(&block)
end

parallelize_teardown(&block)

并行测试的清理钩子。如果你使用的数据库是写/读分离的,或者在测试完成前需要进行其他清理工作,可以使用此钩子来删除数据库。这会在 fork 进程关闭前运行。

注意:此功能在线程并行化中不可用。

在你的 test_helper.rb 中添加以下内容

class ActiveSupport::TestCase
  parallelize_teardown do
    # drop databases
  end
end
# File activesupport/lib/active_support/test_case.rb, line 166
def parallelize_teardown(&block)
  ActiveSupport::Testing::Parallelization.run_cleanup_hook(&block)
end

test_order()

返回测试用例运行的顺序。

ActiveSupport::TestCase.test_order # => :random

可能的值有 :random:parallel:alpha:sorted。默认为 :random

# File activesupport/lib/active_support/test_case.rb, line 61
def test_order
  ActiveSupport.test_order ||= :random
end

test_order=(new_order)

设置测试用例运行的顺序。

ActiveSupport::TestCase.test_order = :random # => :random

有效值为

  • :random (以随机顺序运行测试)

  • :parallel (并行运行测试)

  • :sorted (按方法名字母顺序运行测试)

  • :alpha (等同于 :sorted)

# File activesupport/lib/active_support/test_case.rb, line 51
def test_order=(new_order)
  ActiveSupport.test_order = new_order
end

实例公共方法

assert_no_match(matcher, obj, msg = nil)

别名: refute_match

# File activesupport/lib/active_support/test_case.rb, line 288
    

assert_not_empty(obj, msg = nil)

别名: refute_empty

# File activesupport/lib/active_support/test_case.rb, line 211
    

assert_not_equal(exp, act, msg = nil)

别名: refute_equal

# File activesupport/lib/active_support/test_case.rb, line 222
    

assert_not_in_delta(exp, act, delta = 0.001, msg = nil)

别名: refute_in_delta

# File activesupport/lib/active_support/test_case.rb, line 233
    

assert_not_in_epsilon(a, b, epsilon = 0.001, msg = nil)

# File activesupport/lib/active_support/test_case.rb, line 244
    

assert_not_includes(collection, obj, msg = nil)

别名: refute_includes

# File activesupport/lib/active_support/test_case.rb, line 255
    

assert_not_instance_of(cls, obj, msg = nil)

# File activesupport/lib/active_support/test_case.rb, line 266
    

assert_not_kind_of(cls, obj, msg = nil)

别名: refute_kind_of

# File activesupport/lib/active_support/test_case.rb, line 277
    

assert_not_nil(obj, msg = nil)

别名: refute_nil

# File activesupport/lib/active_support/test_case.rb, line 299
    

assert_not_operator(o1, op, o2 = UNDEFINED, msg = nil)

别名: refute_operator

# File activesupport/lib/active_support/test_case.rb, line 310
    

assert_not_predicate(o1, op, msg = nil)

# File activesupport/lib/active_support/test_case.rb, line 321
    

assert_not_respond_to(obj, meth, msg = nil)

# File activesupport/lib/active_support/test_case.rb, line 332
    

assert_not_same(exp, act, msg = nil)

别名: refute_same

# File activesupport/lib/active_support/test_case.rb, line 343
    

parallel_worker_id()

如果测试正在并行运行,则返回当前的并行工作进程 ID

# File activesupport/lib/active_support/test_case.rb, line 193
def parallel_worker_id
  self.class.parallel_worker_id
end