Skip to content

Instantly share code, notes, and snippets.

@chrisnicola
Last active August 3, 2016 11:49
Show Gist options
  • Save chrisnicola/c45e73053b222ebda36bae27ed5d078e to your computer and use it in GitHub Desktop.
Save chrisnicola/c45e73053b222ebda36bae27ed5d078e to your computer and use it in GitHub Desktop.
Examples of trying to set test order to be sorted. Note that I'm looking at the order that test files are being run as well. Perhaps that isn't controlled here.
# So far I have tried the following code in test_helper.rb
# Based on documentation here: http://api.rubyonrails.org/v4.2/classes/ActiveSupport/TestCase.html
class ActiveSupport::TestCase
def test_order
:sorted
end
end
# Realizing that it is a class method I changed this to
ActiveSupport::TestCase.test_order = :sorted
# Regardless in Rails 4 the default test order is supposed to be :sorted
# while in Rails 5 it changes to :random
#
# Also checking manually using RAILS_ENV=test rails c I can see that
# ActiveSupport::TestCase is fact returning :sorted
# Moving on to MiniTest directly
class MiniTest::Test
def test_order
:sorted
end
end
class MiniTest::Test
def self.test_order
:sorted
end
end
# Both of these seem to have no impact as well. Both the order the test files
# are executed and the order of tests within the files are still randomized.
# What I have had luck with is fixing the seed value when running locally.
# I have also tried adding `i_suck_and_my_tests_are_order_dependent!` to the
# TestCase base class for good measure, but I have no intention of checking
# in any code like that.
# Also considered that perhaps I was setting this too late and moved the
# code to `config/environment/test.rb`. I can confirm via pry breakpoint
# that MiniTest::Test.test_order == :sorted but tests continue to run in
# random order.
@chrisnicola
Copy link
Author

Ok I've tracked this down to minitest-spec-rails. I'm not entirely sure why it breaks the override behaviour, but I was able to get consistent test order without it in a new project using both ActiveSupport::TestCase and MiniTest::Test based tests.

@chrisnicola
Copy link
Author

I've opened a bug here metaskills/minitest-spec-rails#76

@jrafanie
Copy link

jrafanie commented Aug 3, 2016

Cool, thanks for tracking it down @chrisnicola. It would be nice to update minitest-spec-rails's readme if there's something you tripped on that could save others the same headache down the road, or that the very least, having your issue in the repo could help others. Thanks for taking the time to dig into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment