2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2016-08-06 11:58:50 -04:00
|
|
|
gem "minitest" # make sure we get the gem, not stdlib
|
|
|
|
require "minitest"
|
2017-10-21 09:11:29 -04:00
|
|
|
require "active_support/testing/tagged_logging"
|
|
|
|
require "active_support/testing/setup_and_teardown"
|
|
|
|
require "active_support/testing/assertions"
|
|
|
|
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"
|
|
|
|
require "active_support/testing/file_fixtures"
|
2017-12-20 16:59:41 -05:00
|
|
|
require "active_support/testing/parallelization"
|
2008-11-23 17:48:36 -05:00
|
|
|
|
2008-11-22 22:18:30 -05:00
|
|
|
module ActiveSupport
|
2013-05-06 20:38:45 -04:00
|
|
|
class TestCase < ::Minitest::Test
|
|
|
|
Assertion = Minitest::Assertion
|
|
|
|
|
2014-08-12 09:51:41 -04:00
|
|
|
class << self
|
2014-12-17 17:55:15 -05: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 08:32:16 -04:00
|
|
|
def test_order=(new_order)
|
2014-09-10 23:54:43 -04:00
|
|
|
ActiveSupport.test_order = new_order
|
2014-09-08 08:32:16 -04:00
|
|
|
end
|
|
|
|
|
2014-12-17 17:55:15 -05:00
|
|
|
# Returns the order in which test cases are run.
|
|
|
|
#
|
2015-01-02 20:43:06 -05:00
|
|
|
# ActiveSupport::TestCase.test_order # => :random
|
2014-12-17 17:55:15 -05:00
|
|
|
#
|
|
|
|
# Possible values are +:random+, +:parallel+, +:alpha+, +:sorted+.
|
2015-01-02 20:43:06 -05:00
|
|
|
# Defaults to +:random+.
|
2014-09-08 08:32:16 -04:00
|
|
|
def test_order
|
2015-05-04 07:46:12 -04:00
|
|
|
ActiveSupport.test_order ||= :random
|
2014-09-08 08:32:16 -04:00
|
|
|
end
|
2017-12-20 16:59:41 -05:00
|
|
|
|
|
|
|
# Parallelizes the test suite.
|
|
|
|
#
|
2018-04-02 19:55:08 -04:00
|
|
|
# Takes a +workers+ argument that controls how many times the process
|
2017-12-20 16:59:41 -05: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 19:55:08 -04:00
|
|
|
# If <tt>ENV["PARALLEL_WORKERS"]</tt> is set the workers argument will be ignored
|
2017-12-20 16:59:41 -05: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 19:55:08 -04:00
|
|
|
# If the number of workers is set to +1+ or fewer, the tests will not be
|
2017-12-20 16:59:41 -05:00
|
|
|
# parallelized.
|
|
|
|
#
|
|
|
|
# The default parallelization method is to fork processes. If you'd like to
|
2018-04-02 19:55:08 -04:00
|
|
|
# use threads instead you can pass <tt>with: :threads</tt> to the +parallelize+
|
2017-12-20 16:59:41 -05:00
|
|
|
# method. Note the threaded parallelization does not create multiple
|
|
|
|
# database and will not work with system tests at this time.
|
|
|
|
#
|
|
|
|
# parallelize(workers: 2, with: :threads)
|
|
|
|
#
|
2018-09-11 17:52:27 -04:00
|
|
|
# The threaded parallelization uses minitest's parallel executor directly.
|
|
|
|
# The processes parallelization uses a Ruby DRb server.
|
2017-12-20 16:59:41 -05:00
|
|
|
def parallelize(workers: 2, with: :processes)
|
|
|
|
workers = ENV["PARALLEL_WORKERS"].to_i if ENV["PARALLEL_WORKERS"]
|
|
|
|
|
|
|
|
return if workers <= 1
|
|
|
|
|
|
|
|
executor = case with
|
|
|
|
when :processes
|
|
|
|
Testing::Parallelization.new(workers)
|
|
|
|
when :threads
|
|
|
|
Minitest::Parallel::Executor.new(workers)
|
|
|
|
else
|
2018-02-17 19:01:20 -05:00
|
|
|
raise ArgumentError, "#{with} is not a supported parallelization executor."
|
2017-12-20 16:59:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
self.lock_threads = false if defined?(self.lock_threads) && with == :threads
|
|
|
|
|
|
|
|
Minitest.parallel_executor = executor
|
|
|
|
|
|
|
|
parallelize_me!
|
|
|
|
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)
|
|
|
|
ActiveSupport::Testing::Parallelization.after_fork_hook do |worker|
|
|
|
|
yield worker
|
|
|
|
end
|
|
|
|
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)
|
|
|
|
ActiveSupport::Testing::Parallelization.run_cleanup_hook do |worker|
|
|
|
|
yield worker
|
|
|
|
end
|
|
|
|
end
|
2014-08-12 09:51:41 -04:00
|
|
|
end
|
|
|
|
|
2013-05-06 20:38:45 -04:00
|
|
|
alias_method :method_name, :name
|
2008-11-07 12:45:48 -05:00
|
|
|
|
2012-09-26 14:16:43 -04:00
|
|
|
include ActiveSupport::Testing::TaggedLogging
|
2018-04-26 14:38:02 -04:00
|
|
|
prepend ActiveSupport::Testing::SetupAndTeardown
|
2008-11-07 12:45:48 -05:00
|
|
|
include ActiveSupport::Testing::Assertions
|
2008-11-23 16:11:07 -05:00
|
|
|
include ActiveSupport::Testing::Deprecation
|
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 13:03:28 -05:00
|
|
|
include ActiveSupport::Testing::TimeHelpers
|
2015-01-23 09:45:34 -05:00
|
|
|
include ActiveSupport::Testing::FileFixtures
|
2012-12-28 12:06:10 -05:00
|
|
|
extend ActiveSupport::Testing::Declarative
|
2012-01-05 18:46:17 -05:00
|
|
|
|
|
|
|
# test/unit backwards compatibility methods
|
|
|
|
alias :assert_raise :assert_raises
|
2012-12-28 18:49:41 -05:00
|
|
|
alias :assert_not_empty :refute_empty
|
2012-01-05 18:46:17 -05:00
|
|
|
alias :assert_not_equal :refute_equal
|
2012-12-28 18:49:41 -05:00
|
|
|
alias :assert_not_in_delta :refute_in_delta
|
|
|
|
alias :assert_not_in_epsilon :refute_in_epsilon
|
|
|
|
alias :assert_not_includes :refute_includes
|
|
|
|
alias :assert_not_instance_of :refute_instance_of
|
|
|
|
alias :assert_not_kind_of :refute_kind_of
|
2012-01-05 18:46:17 -05:00
|
|
|
alias :assert_no_match :refute_match
|
2012-12-28 18:49:41 -05:00
|
|
|
alias :assert_not_nil :refute_nil
|
|
|
|
alias :assert_not_operator :refute_operator
|
|
|
|
alias :assert_not_predicate :refute_predicate
|
|
|
|
alias :assert_not_respond_to :refute_respond_to
|
2012-01-06 17:04:09 -05:00
|
|
|
alias :assert_not_same :refute_same
|
2016-10-21 17:44:17 -04:00
|
|
|
|
|
|
|
ActiveSupport.run_load_hooks(:active_support_test_case, self)
|
2008-11-07 12:45:48 -05:00
|
|
|
end
|
2008-11-22 22:18:30 -05:00
|
|
|
end
|