Commit Graph

119 Commits

Author SHA1 Message Date
David Heinemeier Hansson
fdb98d218e
Add TestCase#stub_const (#44294)
* Add TestCase#stub_const

* Note the concurrency issue

* Changelog entry
2022-02-01 12:20:06 +01:00
Jean Boussier
c91c266872 Enable Style/ExplicitBlockArgument cop
This reduce the stack size which is beneficial for
exceptions performance.

See: https://gist.github.com/byroot/cb3bcadcc3701c2518d002fb8d3a4e7a

However the cop is unsafe because it might change the block arity,
so it can run into some false positives.
2021-09-05 17:06:19 +02:00
Jorge Manrubia
a29884d733 Remove mechanism to disable test parallelization when runnin gonly 1 test
#42761 made the old system of disable paralleling testing when
only one test file was included obsolete.
2021-07-27 10:33:09 -07:00
Jorge Manrubia
675d9ffb6e
Add an option threshold: to .parallel() setup method (#42789)
This adds an additional method to configure the parallelization
threshold. Before this, the only way of configuring the threshold was
via an option:

```
config.active_support.test_parallelization_minimum_number_of_tests
```
2021-07-16 11:32:23 -07:00
Jorge Manrubia
ecc5afed30 Parallelize tests only when overhead is justified
Parallelizing tests has a cost in terms of database setup and fixture
loading. This change makes Rails disable parallelization when the number
of tests is below a configurable threshold.

When running tests in parallel each process gets its own database
instance. On each execution, each process will update each database
schema (if needed) and load all the fixtures. This can be very expensive
for non trivial datasets.

As an example, for HEY, when running a single file with 18 tests,
running tests in parallel in my box adds an overhead of 13 seconds
versus not parallelizing them. Of course parallelizing is totally worthy
when there are many tests to run, but not when running just a few tests.

The threshold is configurable via
config.active_support.test_parallelization_minimum_number_of_tests,
which is 30 50 by default.

This also adds some tracing to know how tests are being executed:

When in parallel:

```
Running 2829 tests in parallel in 8 processes
```

When not in parallel:

```
Running 15 tests in a single process (parallelization threshold is 30)
```
2021-07-14 13:36:28 +02:00
Lee Quarella
65f350166a Remove overwriting test_order
Reverts a change from
2327ebfdc6
which can overwrite `test_order` that may have been manually set in
config. This can cause a situation where the user is depending on a
particular `test_order` but is unknowingly forced into another.
2021-03-31 11:50:08 -04:00
Ricardo Díaz
2327ebfdc6 Disable parallel testing when running individual files
Setting up the parallel workers could be an overhead when running
individual files.

This patch disables that process in case the number of files to run
is less than one.

Results running a sample file:

Before:

```
actionpack $ bin/test test/controller/parameters/accessors_test.rb
Run options: --seed 48261

........................................................................

Finished in 0.211923s, 339.7460 runs/s, 552.0873 assertions/s.
72 runs, 117 assertions, 0 failures, 0 errors, 0 skips
```

After

```
actionpack $ bin/test test/controller/parameters/accessors_test.rb

Run options: --seed 5461

........................................................................

Finished in 0.008411s, 8560.2189 runs/s, 13910.3557 assertions/s.
72 runs, 117 assertions, 0 failures, 0 errors, 0 skips
```
2021-03-18 02:06:42 -05:00
Rafael Mendonça França
967beb7229
Revert "MethodCallAssertions is a regular player of the team ActiveSupport::TestCase now"
This reverts commit 98d0f7ebd34b858f12a12dcf37ae54fdbb5cab64.
2019-08-02 00:24:21 -04:00
Rafael Mendonça França
6384933994
Revert "You give jruby_skip & rubinius_skip a good name"
This reverts commit 8d2866bb80fbe81acb04f5b0c44f152f571fb29f.
2019-08-02 00:24:11 -04:00
Akira Matsuda
8d2866bb80 You give jruby_skip & rubinius_skip a good name
This hack prevails everywhere in the codebase by being copy & pasted, and it's actually not a negative thing but a necessary thing for framework implementors,
so it should better have a name and be a thing.

And with this commit, activesupport/test/abstract_unit.rb now doesn't silently autoload AS::TestCase,
so we're ready to establish clearner environment for running AS tests (probably in later commits)
2019-08-02 05:36:38 +09:00
Akira Matsuda
98d0f7ebd3 MethodCallAssertions is a regular player of the team ActiveSupport::TestCase now
It's used everywhere, clean and mature enough
2019-08-02 05:36:15 +09:00
Bogdan
e9f6ce617b Add option to set parallel test worker count to the physical core count of the machine (#34735)
* Add option to set parallel test worker count to the physical core count of the machine

Also, use the physical core count of the machine as
the default number of workers, and  generate the `test_helper.rb` file
with `parallelize(workers: :number_of_processors)`

Closes #34734

* Ensure that we always test parallel testing

Since #34734 we decided to use the physical core count of the machine as
the default number of workers in the parallel testing, we need to
ensure that some tests use at least 2 workers because we could
run those tests on VM that has only 1 physical core.
It also fixes tests failures on the CI since Travis server we are using
has only one physical core.
See https://travis-ci.org/rails/rails/jobs/469281088#L2352
2018-12-18 10:25:35 -08:00
Xavier Noria
f589e20b0a use "minitest" consistently
The name of the minitest library is spelled that way: regular font, and
lowercase. Lowercase is used even at the beginning of sentences, see

    http://docs.seattlerb.org/minitest/

I double-checked this with @zenspider too (thanks!).
2018-09-11 23:57:25 +02:00
Edouard CHIN
1cf8b6c231 SetupAndTeardown has few caveats that breaks libraries:
- In #32472 I introduced a fix in order for all `after_teardown` method provided by libraries and Rails to run, even if the application's `teardown` method raised an error (That's the default minitest behavior). However this change wasn't enough and doesn't take in consideration the ancestors chain.
  If a library's module containing an `after_teardown` method get included after the `SetupAndTeardown` module (one example is the [ActiveRecord::TestFixtures module](7d2400ab61/activerecord/lib/active_record/fixtures.rb (L855-L856)), then the ancestors of the test class would look something like
  ```ruby
    class MyTest < ActiveSupport::TestCase
    end

    puts MyTest.ancestors # [MyTest, ActiveSupport::TestCase, ActiveRecord::TestFixtures, ActiveSupport::Testing::SetupAndTeardown]
  ```
  Any class/module in the ancestors chain that are **before** the `ActiveSupport::Testing::SetupAndTeardown` will behave incorrectly:
    - Their `before_setup` method will get called **after** all regular setup method
    - Their `after_teardown` method won't even get called in case an exception is raised inside a regular's test `teardown`

  A simple reproduction script of the problem here https://gist.github.com/Edouard-chin/70705542a59a8593f619b02e1c0a188c

- One solution to this problem is to have the `AS::SetupAndTeardown` module be the very first in the ancestors chain. By doing that we ensure that no `before_setup` / `after_teardown` get executed prior to running the teardown callbacks
2018-04-27 01:36:27 -04:00
T.J. Schuck
0ef8221910 Small doc fixes
[ci skip]
2018-04-02 19:55:08 -04:00
Dharam Gollapudi
a4e226fb2d Fixes typos
Fixes typos
2018-02-17 16:03:25 -08:00
eileencodes
26821d9b57 Add test parallelization to Rails
Provides both a forked process and threaded parallelization options. To
use add `parallelize` to your test suite.

Takes a `workers` argument that controls how many times the process
is forked. For each process a new database will be created suffixed
with the worker number; test-database-0 and test-database-1
respectively.

If `ENV["PARALLEL_WORKERS"]` is set the workers argument will be ignored
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.

If the number of workers is set to `1` or fewer, the tests will not be
parallelized.

The default parallelization method is to fork processes. If you'd like to
use threads instead you can pass `with: :threads` to the `parallelize`
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)

The threaded parallelization uses Minitest's parallel exector directly.
The processes paralleliztion uses a Ruby Drb server.

For parallelization via threads a setup hook and cleanup hook are
provided.

```
class ActiveSupport::TestCase
  parallelize_setup do |worker|
    # setup databases
  end

  parallelize_teardown do |worker|
    # cleanup database
  end

  parallelize(workers: 2)
end
```

[Eileen M. Uchitelle, Aaron Patterson]
2018-02-15 19:21:24 -05:00
yuuji.yaginuma
aec2b8b363 Remove unused require
This is no longer used since fd6aaaa.
2017-11-10 15:37:31 +09:00
Akira Matsuda
589dd0f6c9 [Active Support] require_relative => require
This basically reverts 8da30ad6be34339124ba4cb4e36aea260dda12bc
2017-10-21 22:48:27 +09:00
Koichi ITO
ac717d65a3 [Active Support] rubocop -a --only Layout/EmptyLineAfterMagicComment 2017-07-11 13:12:32 +09:00
Kir Shatrov
72950568dd Use frozen-string-literal in ActiveSupport 2017-07-09 15:08:29 +03:00
Akira Matsuda
8da30ad6be [Active Support] require => require_relative 2017-07-01 18:38:04 +09:00
Aaron Patterson
b6f935bbf9
Use on_load to trigger commandline processing code
We need to use on_load so that plugins will get the same functionality
2016-10-21 14:44:17 -07:00
Santosh Wadghule
3cece0b657 Move custom assertion to its proper place
ActiveSupport::Testing::Assertions.

We have a separate module in which have defined Rails' own custom
assertions. So it would be good to keep all custom Rails' assertions in
one place i.e. in this module.
2016-08-27 08:33:08 +05:30
Ryuta Kamizono
762e3f05f3 Add Style/EmptyLines in .rubocop.yml and remove extra empty lines 2016-08-07 17:50:59 +09:00
Xavier Noria
d66e7835be applies new string literal convention in activesupport/lib
The current code base is not uniform. After some discussion,
we have chosen to go with double quotes by default.
2016-08-06 18:10:53 +02:00
Rafael Mendonça França
4a7cd700b4
Remove deprecated arguments in assert_nothing_raised 2016-06-13 12:05:33 -04:00
Ryuta Kamizono
63171b86cb Fix assert_nothing_raised deprecation warning format
Before:

```
DEPRECATION WARNING: Passing arguments to assert_nothing_raised
        is deprecated and will be removed in Rails 5.1.
```

After:

```
DEPRECATION WARNING: Passing arguments to assert_nothing_raised is deprecated and will be removed in Rails 5.1.
```
2016-02-24 09:21:06 +09:00
Tara Scherner de la Fuente
d96550b53e add deprecation warning to assert_nothing_raised and changelog entry 2016-02-22 22:56:23 -08:00
Tara Scherner de la Fuente
926a24a751 remove args from assert_nothing_raised in tests 2016-02-22 22:56:23 -08:00
Tara Scherner de la Fuente
f672464faf better docs for ActiveSupport::TestCase#assert_nothing_raised 2016-02-18 23:00:06 -08:00
Kasper Timm Hansen
69e5547162 Extract line filtering to Railties.
The line filter parsing added to ActiveSupport::TestCase is only half the story
to enable line filtering. The other half, of adding the patterns to the options,
is done in the Minitest plugin that Railties has.

Thus it makes more sense to have the filter in Railties with the other half and
all the line filtering tests.

Move the filter and extend Active Support in an initializer, so that when users
or `rails/all.rb` require `rails/test_unit/railtie` we can still filter by line.
2016-01-09 17:21:21 +01:00
Kasper Timm Hansen
b6fc8e25a1 Improve test runner's Minitest integration.
This also adds free mix and matching of directories, files and lines filters.
Like so:

bin/rails test models/post_test.rb test/integration models/person_test.rb:26

You can also mix in a traditional Minitest filter:

bin/rails test test/integration -n /check_it_out/
2015-06-04 20:57:08 +02:00
Mehmet Emin İNAÇ
bf501e7ff5 refactor ActiveSupport::TestCase.test_order method with memoization 2015-05-04 14:46:12 +03:00
Guo Xiang Tan
ea5e3192d5 Fix incorrect description for assert_nothing_raised. 2015-03-24 15:32:13 +08:00
Guo Xiang Tan
5a973b314b Remove alias for i_suck_and_my_tests_are_order_dependent. 2015-03-24 00:27:53 +08:00
Yves Senn
d28e5b94a7 introduce ActiveSupport::Testing::FileFixtures.
It's a thin layer to provide easy access to sample files throughout
test-cases. This adds the directory `test/fixtures/files` to newly
generated applications.
2015-01-28 12:29:34 +01:00
claudiob
adfeeb01e7 Remove unneeded require 'as/deprecation'
Tests should still pass after removing `require 'active_support/deprecation'`
from these files since the related deprecations have been removed.
2015-01-04 07:45:07 -08:00
Rafael Mendonça França
5f777e4b5e Change the default test order from :sorted to :random 2015-01-04 11:58:41 -03:00
claudiob
1d19a3a42a Add docs for AS::TestCase::test_order
Document `test_order` and `test_order=` from `ActiveSupport::TestCase`.

[ci skip]
2014-12-18 11:20:32 -08:00
Xavier Noria
e595d91ac2 edit pass over all warnings
This patch uniformizes warning messages. I used the most common style
already present in the code base:

* Capitalize the first word.

* End the message with a full stop.

* "Rails 5" instead of "Rails 5.0".

* Backticks for method names and inline code.

Also, converted a few long strings into the new heredoc convention.
2014-10-28 17:47:32 -07:00
Rafael Mendonça França
53e877f7d9 Define the configuration at Active Support 2014-09-11 00:54:43 -03:00
Godfrey Chan
2b41343c34 Default to sorting user's test cases for now
Goals:

1. Default to :random for newly generated applications
2. Default to :sorted for existing applications with a warning
3. Only show the warning once
4. Only show the warning if the app actually uses AS::TestCase

Fixes #16769
2014-09-08 05:32:16 -07:00
Rafael Mendonça França
e81f3c210e Nobody sucks so nobody should call this awful method name 2014-08-12 10:51:41 -03:00
Carlos Antonio da Silva
90745897b9 Remove old setup from AS test case
This was added back in Rails 3 on
c4a6109286909c394e8c5bfc471a1eb9de245d2b, and is not being used anymore.
2014-08-12 07:51:05 -03:00
Akira Matsuda
6ffb29d24e users_dont_suck_but_only_we_suck_and_only_our_tests_are_order_dependent!
Calling ActiveSupport::TestCase.i_suck_and_my_tests_are_order_dependent! in AS::TestCase makes
everyone's tests order dependent, which should never be done by the framework.
2014-08-12 19:37:04 +09:00
Santiago Pastorino
4efb36e7b4 Revert "Merge pull request #15305 from tgxworld/remove_unnecessary_require"
This reverts commit f632f79b8dcd144408c66a544984b2ba9cf52f87, reversing
changes made to 98c7fe87690ca4de6c46e8f69806e82e3f8af42d.

Closes #16343
2014-07-30 09:46:33 -03:00
Rafael Mendonça França
fd6aaaa0c3 Stop requiring mocha automatically
We are planning to remove mocha from our test suite because of
performance problems. To make this possible we should stop require mocha
on ActionSupport::TestCase.

This should not affect applications since users still need to add mocha
to Gemfile and this already load mocha.

Added FIXME notes to place that still need mocha removal
2014-07-19 17:35:12 -03:00
Guo Xiang Tan
4ca5a5ea67 Remove unnecessary require of Minitest.
Minitest has already been required when calling Minitest.autorun.
2014-05-24 20:36:05 -07:00
Prem Sichanugrist
225cd915cf 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-20 16:05:02 +07:00