0

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.

5
  • I will suggest using FactoryBot, that way you generate factories for each model and use them during testing. Commented May 29, 2022 at 18:04
  • Hi Christos! The point is I just created all the fixtures ;0) I will revisit FactoryBot, since I do not remember, if it solves also the parallelised testing infrastructure or just the data for the tests ... Nevertheless I am curious how to set up multiple test_db as suggested in the rails guides. Commented May 29, 2022 at 18:26
  • It solves your problem as you specify the data you need for the test inside the file so you can run them all in parallel without caring about conflicts. By parallel I mean use a cluster and split your tests so each thread runs a portion of the data. Commented May 29, 2022 at 18:51
  • FactoryBot doesn't solve problem with parallel tests. Using only one database would cause interferences between several parallely executed tests.. Please can you add how your database.yml looks like for test environment. If you set parallelize(workers: 1) do your tests work? Thing is It should work automatically provided your database credentials have proper rights to create new databases. Rails tests should handle all these databases for you, you shouldn't have to specify them in database.yml Commented May 29, 2022 at 23:05
  • The tests work. For the time being I activated parallelize(with: :threads) but would like to configure the multiple DB setting for performance reasons. Commented May 30, 2022 at 5:07

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.