跳至内容 跳至搜索
方法
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 121
      

fixture_paths=(fixture_paths)

将给定的路径设置为固定装置集。

也可以追加多个路径。

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

在您的 test_helper.rb 中,您必须有 require "rails/test_help"

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

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

并行化测试套件。

接受一个 workers 参数,该参数控制进程被分叉的次数。对于每个进程,将创建一个新的数据库,并在其后缀中添加工作程序编号。

test-database-0
test-database-1

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

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

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

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

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

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

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

# File activesupport/lib/active_support/test_case.rb, line 80
def parallelize(workers: :number_of_processors, with: :processes, threshold: ActiveSupport.test_parallelization_threshold)
  workers = Concurrent.physical_processor_count if workers == :number_of_processors
  workers = ENV["PARALLEL_WORKERS"].to_i if ENV["PARALLEL_WORKERS"]

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

parallelize_setup(&block)

并行测试的设置钩子。如果有多个数据库或任何需要在派生进程后但在测试运行之前执行的行为,可以使用它。

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

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

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

parallelize_teardown(&block)

并行测试的清理钩子。这可用于在测试完成后删除数据库(如果您的应用程序使用多个读写数据库)或进行其他清理。这在派生进程关闭之前运行。

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

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

class ActiveSupport::TestCase
  parallelize_teardown do
    # drop databases
  end
end
# File activesupport/lib/active_support/test_case.rb, line 117
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 43
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 33
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 231
    

assert_not_empty(obj, msg = nil)

别名:refute_empty

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

assert_not_equal(exp, act, msg = nil)

别名:refute_equal

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

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

别名:refute_in_delta

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

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

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

assert_not_includes(collection, obj, msg = nil)

别名:refute_includes

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

assert_not_instance_of(cls, obj, msg = nil)

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

assert_not_kind_of(cls, obj, msg = nil)

别名:refute_kind_of

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

assert_not_nil(obj, msg = nil)

别名:refute_nil

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

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

别名:refute_operator

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

assert_not_predicate(o1, op, msg = nil)

别名:refute_predicate

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

assert_not_respond_to(obj, meth, msg = nil)

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

assert_not_same(exp, act, msg = nil)

别名:refute_same

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