2017-07-09 12:06:36 +00:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 13:39:13 +00:00
|
|
|
|
2016-08-06 15:58:50 +00:00
|
|
|
require "minitest"
|
2017-10-21 13:11:29 +00:00
|
|
|
require "active_support/testing/tagged_logging"
|
|
|
|
require "active_support/testing/setup_and_teardown"
|
2024-04-30 21:43:54 +00:00
|
|
|
require "active_support/testing/tests_without_assertions"
|
2017-10-21 13:11:29 +00:00
|
|
|
require "active_support/testing/assertions"
|
2022-09-14 10:14:06 +00:00
|
|
|
require "active_support/testing/error_reporter_assertions"
|
2017-10-21 13:11:29 +00:00
|
|
|
require "active_support/testing/deprecation"
|
|
|
|
require "active_support/testing/declarative"
|
|
|
|
require "active_support/testing/isolation"
|
|
|
|
require "active_support/testing/constant_lookup"
|
|
|
|
require "active_support/testing/time_helpers"
|
2022-02-01 11:20:06 +00:00
|
|
|
require "active_support/testing/constant_stubbing"
|
2017-10-21 13:11:29 +00:00
|
|
|
require "active_support/testing/file_fixtures"
|
2017-12-20 21:59:41 +00:00
|
|
|
require "active_support/testing/parallelization"
|
2021-07-12 15:37:52 +00:00
|
|
|
require "active_support/testing/parallelize_executor"
|
2018-12-18 18:25:35 +00:00
|
|
|
require "concurrent/utility/processor_counter"
|
2008-11-23 22:48:36 +00:00
|
|
|
|
2008-11-23 03:18:30 +00:00
|
|
|
module ActiveSupport
|
2013-05-07 00:38:45 +00:00
|
|
|
class TestCase < ::Minitest::Test
|
|
|
|
Assertion = Minitest::Assertion
|
|
|
|
|
2014-08-12 13:51:41 +00:00
|
|
|
class << self
|
2014-12-17 22:55:15 +00:00
|
|
|
# Sets the order in which test cases are run.
|
|
|
|
#
|
|
|
|
# ActiveSupport::TestCase.test_order = :random # => :random
|
|
|
|
#
|
|
|
|
# Valid values are:
|
|
|
|
# * +:random+ (to run tests in random order)
|
|
|
|
# * +:parallel+ (to run tests in parallel)
|
|
|
|
# * +:sorted+ (to run tests alphabetically by method name)
|
|
|
|
# * +:alpha+ (equivalent to +:sorted+)
|
2014-09-08 12:32:16 +00:00
|
|
|
def test_order=(new_order)
|
2014-09-11 03:54:43 +00:00
|
|
|
ActiveSupport.test_order = new_order
|
2014-09-08 12:32:16 +00:00
|
|
|
end
|
|
|
|
|
2014-12-17 22:55:15 +00:00
|
|
|
# Returns the order in which test cases are run.
|
|
|
|
#
|
2015-01-03 01:43:06 +00:00
|
|
|
# ActiveSupport::TestCase.test_order # => :random
|
2014-12-17 22:55:15 +00:00
|
|
|
#
|
|
|
|
# Possible values are +:random+, +:parallel+, +:alpha+, +:sorted+.
|
2015-01-03 01:43:06 +00:00
|
|
|
# Defaults to +:random+.
|
2014-09-08 12:32:16 +00:00
|
|
|
def test_order
|
2015-05-04 11:46:12 +00:00
|
|
|
ActiveSupport.test_order ||= :random
|
2014-09-08 12:32:16 +00:00
|
|
|
end
|
2017-12-20 21:59:41 +00:00
|
|
|
|
|
|
|
# Parallelizes the test suite.
|
|
|
|
#
|
2018-04-02 23:55:08 +00:00
|
|
|
# Takes a +workers+ argument that controls how many times the process
|
2017-12-20 21:59:41 +00:00
|
|
|
# is forked. For each process a new database will be created suffixed
|
|
|
|
# with the worker number.
|
|
|
|
#
|
|
|
|
# test-database-0
|
|
|
|
# test-database-1
|
|
|
|
#
|
2018-04-02 23:55:08 +00:00
|
|
|
# If <tt>ENV["PARALLEL_WORKERS"]</tt> is set the workers argument will be ignored
|
2017-12-20 21:59:41 +00:00
|
|
|
# and the environment variable will be used instead. This is useful for CI
|
|
|
|
# environments, or other environments where you may need more workers than
|
|
|
|
# you do for local testing.
|
|
|
|
#
|
2018-04-02 23:55:08 +00:00
|
|
|
# If the number of workers is set to +1+ or fewer, the tests will not be
|
2017-12-20 21:59:41 +00:00
|
|
|
# parallelized.
|
|
|
|
#
|
2018-12-18 18:25:35 +00:00
|
|
|
# If +workers+ is set to +:number_of_processors+, the number of workers will be
|
|
|
|
# set to the actual core count on the machine you are on.
|
|
|
|
#
|
2017-12-20 21:59:41 +00:00
|
|
|
# The default parallelization method is to fork processes. If you'd like to
|
2018-04-02 23:55:08 +00:00
|
|
|
# use threads instead you can pass <tt>with: :threads</tt> to the +parallelize+
|
2017-12-20 21:59:41 +00:00
|
|
|
# method. Note the threaded parallelization does not create multiple
|
2022-12-13 21:40:30 +00:00
|
|
|
# databases and will not work with system tests.
|
2017-12-20 21:59:41 +00:00
|
|
|
#
|
2018-12-18 18:25:35 +00:00
|
|
|
# parallelize(workers: :number_of_processors, with: :threads)
|
2017-12-20 21:59:41 +00:00
|
|
|
#
|
2018-09-11 21:52:27 +00:00
|
|
|
# The threaded parallelization uses minitest's parallel executor directly.
|
|
|
|
# The processes parallelization uses a Ruby DRb server.
|
2021-07-16 18:32:23 +00:00
|
|
|
#
|
|
|
|
# Because parallelization presents an overhead, it is only enabled when the
|
|
|
|
# number of tests to run is above the +threshold+ param. The default value is
|
|
|
|
# 50, and it's configurable via +config.active_support.test_parallelization_threshold+.
|
|
|
|
def parallelize(workers: :number_of_processors, with: :processes, threshold: ActiveSupport.test_parallelization_threshold)
|
2024-01-03 22:11:06 +00:00
|
|
|
workers = Concurrent.processor_count if workers == :number_of_processors
|
2017-12-20 21:59:41 +00:00
|
|
|
workers = ENV["PARALLEL_WORKERS"].to_i if ENV["PARALLEL_WORKERS"]
|
|
|
|
|
2021-07-16 18:32:23 +00:00
|
|
|
Minitest.parallel_executor = ActiveSupport::Testing::ParallelizeExecutor.new(size: workers, with: with, threshold: threshold)
|
2017-12-20 21:59:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Set up hook for parallel testing. This can be used if you have multiple
|
|
|
|
# databases or any behavior that needs to be run after the process is forked
|
|
|
|
# but before the tests run.
|
|
|
|
#
|
|
|
|
# Note: this feature is not available with the threaded parallelization.
|
|
|
|
#
|
|
|
|
# In your +test_helper.rb+ add the following:
|
|
|
|
#
|
|
|
|
# class ActiveSupport::TestCase
|
|
|
|
# parallelize_setup do
|
|
|
|
# # create databases
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
def parallelize_setup(&block)
|
2021-09-05 14:29:32 +00:00
|
|
|
ActiveSupport::Testing::Parallelization.after_fork_hook(&block)
|
2017-12-20 21:59:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Clean up hook for parallel testing. This can be used to drop databases
|
|
|
|
# if your app uses multiple write/read databases or other clean up before
|
|
|
|
# the tests finish. This runs before the forked process is closed.
|
|
|
|
#
|
|
|
|
# Note: this feature is not available with the threaded parallelization.
|
|
|
|
#
|
|
|
|
# In your +test_helper.rb+ add the following:
|
|
|
|
#
|
|
|
|
# class ActiveSupport::TestCase
|
|
|
|
# parallelize_teardown do
|
|
|
|
# # drop databases
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
def parallelize_teardown(&block)
|
2021-09-05 14:29:32 +00:00
|
|
|
ActiveSupport::Testing::Parallelization.run_cleanup_hook(&block)
|
2017-12-20 21:59:41 +00:00
|
|
|
end
|
2023-06-18 08:40:48 +00:00
|
|
|
|
|
|
|
# :singleton-method: fixture_paths
|
|
|
|
#
|
|
|
|
# Returns the ActiveRecord::FixtureSet collection.
|
|
|
|
#
|
|
|
|
# In your +test_helper.rb+ you must have <tt>require "rails/test_help"</tt>.
|
|
|
|
|
|
|
|
# :singleton-method: fixture_paths=
|
|
|
|
#
|
|
|
|
# :call-seq:
|
|
|
|
# fixture_paths=(fixture_paths)
|
|
|
|
#
|
|
|
|
# Sets the given path to the fixture set.
|
|
|
|
#
|
|
|
|
# Can also append multiple paths.
|
|
|
|
#
|
|
|
|
# ActiveSupport::TestCase.fixture_paths << "component1/test/fixtures"
|
|
|
|
#
|
|
|
|
# In your +test_helper.rb+ you must have <tt>require "rails/test_help"</tt>.
|
2014-08-12 13:51:41 +00:00
|
|
|
end
|
|
|
|
|
2013-05-07 00:38:45 +00:00
|
|
|
alias_method :method_name, :name
|
2008-11-07 17:45:48 +00:00
|
|
|
|
2012-09-26 18:16:43 +00:00
|
|
|
include ActiveSupport::Testing::TaggedLogging
|
2018-04-26 18:38:02 +00:00
|
|
|
prepend ActiveSupport::Testing::SetupAndTeardown
|
2024-04-30 21:43:54 +00:00
|
|
|
prepend ActiveSupport::Testing::TestsWithoutAssertions
|
2008-11-07 17:45:48 +00:00
|
|
|
include ActiveSupport::Testing::Assertions
|
2022-09-14 10:14:06 +00:00
|
|
|
include ActiveSupport::Testing::ErrorReporterAssertions
|
2008-11-23 21:11:07 +00:00
|
|
|
include ActiveSupport::Testing::Deprecation
|
2022-02-01 11:20:06 +00:00
|
|
|
include ActiveSupport::Testing::ConstantStubbing
|
Add `#travel` and `#travel_to` to AS::TestCase
Add `ActiveSupport::Testing::TimeHelpers#travel` and `#travel_to`. These
methods change current time to the given time or time difference by
stubbing `Time.now` and `Date.today` to return the time or date after
the difference calculation, or the time or date that got passed into the
method respectively. These methods also accept a block, which will
return current time back to its original state at the end of the block.
Example for `#travel`:
Time.now # => 2013-11-09 15:34:49 -05:00
travel 1.day
Time.now # => 2013-11-10 15:34:49 -05:00
Date.today # => Sun, 10 Nov 2013
Example for `#travel_to`:
Time.now # => 2013-11-09 15:34:49 -05:00
travel_to Time.new(2004, 11, 24, 01, 04, 44)
Time.now # => 2004-11-24 01:04:44 -05:00
Date.today # => Wed, 24 Nov 2004
Both of these methods also accept a block, which will return the current
time back to its original state at the end of the block:
Time.now # => 2013-11-09 15:34:49 -05:00
travel 1.day do
User.create.created_at # => Sun, 10 Nov 2013 15:34:49 EST -05:00
end
travel_to Time.new(2004, 11, 24, 01, 04, 44) do
User.create.created_at # => Wed, 24 Nov 2004 01:04:44 EST -05:00
end
Time.now # => 2013-11-09 15:34:49 -05:00
This module is included in `ActiveSupport::TestCase` automatically.
2013-11-09 18:03:28 +00:00
|
|
|
include ActiveSupport::Testing::TimeHelpers
|
2015-01-23 14:45:34 +00:00
|
|
|
include ActiveSupport::Testing::FileFixtures
|
2012-12-28 17:06:10 +00:00
|
|
|
extend ActiveSupport::Testing::Declarative
|
2012-01-05 23:46:17 +00:00
|
|
|
|
2023-03-31 00:50:58 +00:00
|
|
|
##
|
|
|
|
# :method: assert_not_empty
|
|
|
|
#
|
|
|
|
# :call-seq:
|
|
|
|
# assert_not_empty(obj, msg = nil)
|
|
|
|
#
|
|
|
|
# Alias for: refute_empty[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_empty]
|
|
|
|
|
|
|
|
#
|
2012-12-28 23:49:41 +00:00
|
|
|
alias :assert_not_empty :refute_empty
|
2023-03-31 00:50:58 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# :method: assert_not_equal
|
|
|
|
#
|
|
|
|
# :call-seq:
|
|
|
|
# assert_not_equal(exp, act, msg = nil)
|
|
|
|
#
|
|
|
|
# Alias for: refute_equal[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_equal]
|
|
|
|
|
|
|
|
#
|
2012-01-05 23:46:17 +00:00
|
|
|
alias :assert_not_equal :refute_equal
|
2023-03-31 00:50:58 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# :method: assert_not_in_delta
|
|
|
|
#
|
|
|
|
# :call-seq:
|
|
|
|
# assert_not_in_delta(exp, act, delta = 0.001, msg = nil)
|
|
|
|
#
|
|
|
|
# Alias for: refute_in_delta[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_in_delta]
|
|
|
|
|
|
|
|
#
|
2012-12-28 23:49:41 +00:00
|
|
|
alias :assert_not_in_delta :refute_in_delta
|
2023-03-31 00:50:58 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# :method: assert_not_in_epsilon
|
|
|
|
#
|
|
|
|
# :call-seq:
|
|
|
|
# assert_not_in_epsilon(a, b, epsilon = 0.001, msg = nil)
|
|
|
|
#
|
|
|
|
# Alias for: refute_in_epsilon[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_in_epsilon]
|
|
|
|
|
|
|
|
#
|
2012-12-28 23:49:41 +00:00
|
|
|
alias :assert_not_in_epsilon :refute_in_epsilon
|
2023-03-31 00:50:58 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# :method: assert_not_includes
|
|
|
|
#
|
|
|
|
# :call-seq:
|
|
|
|
# assert_not_includes(collection, obj, msg = nil)
|
|
|
|
#
|
|
|
|
# Alias for: refute_includes[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_includes]
|
|
|
|
|
|
|
|
#
|
2012-12-28 23:49:41 +00:00
|
|
|
alias :assert_not_includes :refute_includes
|
2023-03-31 00:50:58 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# :method: assert_not_instance_of
|
|
|
|
#
|
|
|
|
# :call-seq:
|
|
|
|
# assert_not_instance_of(cls, obj, msg = nil)
|
|
|
|
#
|
|
|
|
# Alias for: refute_instance_of[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_instance_of]
|
|
|
|
|
|
|
|
#
|
2012-12-28 23:49:41 +00:00
|
|
|
alias :assert_not_instance_of :refute_instance_of
|
2023-03-31 00:50:58 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# :method: assert_not_kind_of
|
|
|
|
#
|
|
|
|
# :call-seq:
|
|
|
|
# assert_not_kind_of(cls, obj, msg = nil)
|
|
|
|
#
|
|
|
|
# Alias for: refute_kind_of[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_kind_of]
|
|
|
|
|
|
|
|
#
|
2012-12-28 23:49:41 +00:00
|
|
|
alias :assert_not_kind_of :refute_kind_of
|
2023-03-31 00:50:58 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# :method: assert_no_match
|
|
|
|
#
|
|
|
|
# :call-seq:
|
|
|
|
# assert_no_match(matcher, obj, msg = nil)
|
|
|
|
#
|
|
|
|
# Alias for: refute_match[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_match]
|
|
|
|
|
|
|
|
#
|
2012-01-05 23:46:17 +00:00
|
|
|
alias :assert_no_match :refute_match
|
2023-03-31 00:50:58 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# :method: assert_not_nil
|
|
|
|
#
|
|
|
|
# :call-seq:
|
|
|
|
# assert_not_nil(obj, msg = nil)
|
|
|
|
#
|
|
|
|
# Alias for: refute_nil[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_nil]
|
|
|
|
|
|
|
|
#
|
2012-12-28 23:49:41 +00:00
|
|
|
alias :assert_not_nil :refute_nil
|
2023-03-31 00:50:58 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# :method: assert_not_operator
|
|
|
|
#
|
|
|
|
# :call-seq:
|
|
|
|
# assert_not_operator(o1, op, o2 = UNDEFINED, msg = nil)
|
|
|
|
#
|
|
|
|
# Alias for: refute_operator[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_operator]
|
|
|
|
|
|
|
|
#
|
2012-12-28 23:49:41 +00:00
|
|
|
alias :assert_not_operator :refute_operator
|
2023-03-31 00:50:58 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# :method: assert_not_predicate
|
|
|
|
#
|
|
|
|
# :call-seq:
|
|
|
|
# assert_not_predicate(o1, op, msg = nil)
|
|
|
|
#
|
|
|
|
# Alias for: refute_predicate[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_predicate]
|
|
|
|
|
|
|
|
#
|
2012-12-28 23:49:41 +00:00
|
|
|
alias :assert_not_predicate :refute_predicate
|
2023-03-31 00:50:58 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# :method: assert_not_respond_to
|
|
|
|
#
|
|
|
|
# :call-seq:
|
|
|
|
# assert_not_respond_to(obj, meth, msg = nil)
|
|
|
|
#
|
|
|
|
# Alias for: refute_respond_to[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_respond_to]
|
|
|
|
|
|
|
|
#
|
2012-12-28 23:49:41 +00:00
|
|
|
alias :assert_not_respond_to :refute_respond_to
|
2023-03-31 00:50:58 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# :method: assert_not_same
|
|
|
|
#
|
|
|
|
# :call-seq:
|
|
|
|
# assert_not_same(exp, act, msg = nil)
|
|
|
|
#
|
|
|
|
# Alias for: refute_same[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_same]
|
|
|
|
|
|
|
|
#
|
2012-01-06 22:04:09 +00:00
|
|
|
alias :assert_not_same :refute_same
|
2016-10-21 21:44:17 +00:00
|
|
|
|
|
|
|
ActiveSupport.run_load_hooks(:active_support_test_case, self)
|
2022-08-24 16:54:02 +00:00
|
|
|
|
|
|
|
def inspect # :nodoc:
|
2022-08-24 17:54:04 +00:00
|
|
|
Object.instance_method(:to_s).bind_call(self)
|
2022-08-24 16:54:02 +00:00
|
|
|
end
|
2008-11-07 17:45:48 +00:00
|
|
|
end
|
2008-11-23 03:18:30 +00:00
|
|
|
end
|