Breakpoint doesn't work when running Rails Minitest in Ruby Test Explorer

background

An extension that handles VS Code automated testing is the Test Explorer UI (https://marketplace.visualstudio.com/items?itemName=hbenl.vscode-test-explorer). The adapter for ruby of this extension is Ruby Test Explorer, which can handle rspec and minitest.

There is also an extension called Ruby that allows you to use Breakpoint and step execution for Ruby. This backend is realized by the gems ruby-debug-ide and debase.

Ruby Test Explorer uses "Ruby" as an extension to drive the debug icon.

Ruby with VSCode.png

Expected figure that seems to be such an image

problem

Breakpoint doesn't work when I run a test from a view in the Test Explorer UI for a minitest that Rails automatically generates. In a little more detail, breakpoints work in the (main) context of the test file, but not in the test block.

VSCode Breakpoint.png

Cause?

When rails new, minitest is designed to perform parallel test by default.

test/test_helper.rb


ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'

class ActiveSupport::TestCase
  # Run tests in parallel with specified workers
  parallelize(workers: :number_of_processors) # <=here! !!

  # 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

According to Rails Guide For example, it seems that this is done in DRb and runs on different instances of RubyVM. If the understanding of the previous figure is correct, it will be a different RubyVM = ruby-debug-ide will be a VM for which debase is not set, so the breakpoint will not work.

I mean, the controller should have a few tests, On the log of Ruby Test Explorer, it seems that information such as 0 successes in all 0 cases is returned.

trial and error

If possible, I want to make "parallelize works when executing normally from Test Explorer, and works as a single when executing debug". However, it was difficult to say from the conclusion.

With environment variables ...

For example, there is a setting to specify the command to be given, so I thought that it could be switched with an environment variable ...

  parallelize(workers: :number_of_processors) unless ENV['TEST_SERIAL'].present?
{
  "rubyTestExplorer.minitestCommand": "TEST_SERIAL=1 ./bin/rake"
}

This specifies the command for normal execution, When I run the debug icon, the result looks like this:

[2020-11-06 17:34:09.425] [INFO] Debugging test(s) ["controllers"] of /.................
[2020-11-06 17:34:09.425] [INFO] Running Ruby tests ["controllers"]
[2020-11-06 17:34:09.425] [INFO] Starting the debug session
[2020-11-06 17:34:09.426] [INFO] Running test file: /............../test/controllers/posts_controller_test.rb
[2020-11-06 17:34:09.426] [INFO] Running command: rdebug-ide --host 127.0.0.1 --port 1234 -- $EXT_DIR/debug_minitest.rb 'test/controllers/posts_controller_test.rb'
...

Unfortunately, Ruby Test Explorer can't customize commands when debugging.

If process is not good, use thread

parallelize(workers: :number_of_processors, with: :threads)

I thought that Thread should work on the same RubyVM ... but apparently it doesn't work.

Temporary measures

After all, if you completely abandon parallel execution, the solution will be solved.

test/test_helper.rb


ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'

class ActiveSupport::TestCase
  # Run tests in parallel with specified workers
  # parallelize(workers: :number_of_processors) # <= comment out or delete this line.

  # 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

Recommended Posts

Breakpoint doesn't work when running Rails Minitest in Ruby Test Explorer
When I run minitest in vscode's Ruby Test Explorer, NoMethodError: undefined method `klass'for ...
[Beginner] When rails s doesn't work
SCSS doesn't work when deploying Rails6 AWS
Error in bundle install when running rails new
Ruby on Rails Tutorial Troublesome notes when running on Windows
The story when the test folder was not created in Rails
[Ruby on Rails] When parameter id acquisition does not work
ruby get.chomp.to_i doesn't work, doesn't load
Puma --Nignx is an escape route when rails s -e production -d doesn't work in the environment
Encoding when getting in Windows + Ruby
Ruby on Rails Japanese-English support i18n
[Rails5] Rspec -Unit test when nesting-
When PyCall doesn't work with PyCall :: PythonNotFound
Ruby methods often used in Rails