minitest / minitest-focus

72 stars 9 forks source link

Cannot load minitest-focus in Rails 5 #13

Closed mrteera closed 7 years ago

mrteera commented 8 years ago

I try to put require 'minitest/autorun' into test_helper.rb. It works but rails test execute twice instead once. (I have 8 tests)

# Running tests with run options --seed 25532:
........

Finished tests in 1.936486s, 4.1312 tests/s, 3.6148 assertions/s.
8 tests, 7 assertions, 0 failures, 0 errors, 0 skips

# Running tests with run options --seed 25532:
.

Finished tests in 0.007167s, 1255.7398 tests/s, 1116.2131 assertions/s.
9 tests, 8 assertions, 0 failures, 0 errors, 0 skips

Also, I don't see mini-focus was loaded in Minitest.extensions while it can see other plugins https://github.com/rails/rails/blob/b326e82dc012d81e9698cb1f402502af1788c1e9/railties/lib/rails/test_unit/minitest_plugin.rb#L97 Here I put puts Minitest.extentions

rails
minitest_reporter
guard_minitest
pride

I'm not sure it's Rails issue or not, so I posted here too: https://github.com/rails/rails/issues/25048#issuecomment-246139483

zenspider commented 8 years ago

pinging @arthurnn

arthurnn commented 8 years ago

So, the problem here is that the rails runner call Minitest.run and don't let minitest/autorun run it. see https://github.com/rails/rails/blob/88ff3f820aa88b7235b7554668f91a73532726af/railties/lib/rails/commands/test.rb

That means that if you do:

require 'minitest/focus'
require 'minitest/autorun'
class AwesomeTest < ActiveSupport::TestCase
end

and run it with bin/rails test awesome_test.rb That will run our runner code, as well as require minitest/autorun, which has another exit Minitest.run call. So that make it run twice. And the reason focus won't work, is that focus mutate the ARGV to make it work, however that's too late as our runner already called Minitest.run(ARGV), and at that point the ARGV was not mutated yet.

I think the solution here is to make minitest-focus work as a plugin, and change the options on the plugin_focus_options method, instead of mutation the ARGV. Does that sound correct @zenspider , any thoughts? I can maybe try to fix that.

zenspider commented 8 years ago

Dunno how it'd go... but I'm open to reviewing it. :)

rhymes commented 7 years ago

Any news? :-)

zxiest commented 7 years ago

@rhymes I have done the following (without minitest-focus)

In my test_helper.rb

class ActiveSupport::TestCase
  class << self
    alias :old_test :test

    def test_all
      @__run_all = true
    end

    def focus
      @__run_next = true
      @__run_all = false
    end

    def test(*args, &block)
      if (@__run_next || @__run_all)
        old_test(*args, &block)

        @__run_next = false
      end
    end
  end
end
require 'test_helper'

class FoobarTest < ActionDispatch::IntegrationTest
  # test_all # uncomment if you want to run all the tests
  focus
  test "bar bar" do
    assert false
  end

  test "foobar test" do
    assert true
  end

  focus
  test "no no no" do
    assert true
  end
end

The only problem is that you'd have to call test_all when you want to run all the tests.

A

rhymes commented 7 years ago

@mrteera @zenspider @arthurnn @zxiest

I just tried with Rails 5.0.5 after this fix https://github.com/rails/rails/blob/v5.0.5/railties/CHANGELOG.md#rails-505rc1-july-19-2017 and it works correctly.

arthurnn commented 7 years ago

@rhymes sweet. I guess this can be closed then