I am working on the tests for an app and just now got beyond 50 tests, which means the parallel testing kicked in. With it I currently get error messages telling me ActiveRecord::ConnectionNotEstablished: Access denied for user 'user'@'localhost' to database 'test_DB-0'
, 'test_DB-1'
, 'test_DB-2'
and so on. That is because I just have one test-DB specified in my database.yml.
I read in the rails guides that apparently I have to use before and after actions in the test_helper.rb
ENV["RAILS_ENV"] ||= "test"
require_relative "../config/environment"
require "rails/test_help"
require "minitest/autorun"
require "minitest/reporters"
Minitest::Reporters.use!
class ActiveSupport::TestCase
parallelize_setup do |worker|
# setup databases
end
parallelize_teardown do |worker|
# setup databases
end
# Run tests in parallel with specified workers
parallelize(workers: :number_of_processors)
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
But how do I do this and can I leave the data for my old test-db in database.yml? Can I get around creating the DBs by hand by using these before and after actions?
Thank you in advance!
Edit
The test section of my database.yml is:
test:
adapter: mysql2
database: <%= ENV["testDB"] %>
username: <%= ENV["testDB-username"] %>
password: <%= ENV["testDB-password"] %>
host: localhost
encoding: utf8
collation: utf8_unicode_ci
The testDB-username on localhost has access to the testDB, but has no global root rights.
Thanks to edariedl's hint I was able set up the parallelised testing. I had to grant global permissions to my dev-sql user. This is different from my usual setting, where I grant permissions only to the dev-database.
After granting global privileges to my dev-sql user:
mysql > GRANT ALL PRIVILEGES ON *.* TO user@localhost;
mysql > FLUSH PRIVILEGES;
Minitest does the rest and forks the testing. It creates n Databases, where n is the amount of cpu-cores of your system.
In my case this kind of parallelisation appears to be more solid and faster than threaded testing.