Search code examples
ruby-on-railssortingtestingcapybara

Is there a way to force the order of tests in Rails?


I have an ideas_test.rb file with 2 tests

test "create new idea" do
end
test "that Ideas are loaded in the index" do
end

When I run rails test test/system/ideas_test.rb, the 2nd test is executed first. Why is that? Is there a way to force the order of tests? Is the DB cleared between each Test?

Thanks in advance.


Solution

  • Test theory says that tests shouldn't have dependencies, meaning that when you run a test it shouldn't depend on another test to pass.

    The best way to make sure that we catch dependencies is to run tests in random order.

    As others said, you can force tests to run in the provided order, but that is considered a bad practice and I highly recommend you to stay away from it.

    Update:

    All fixtures, objects persisted in setup (def setup) and in the test (test "..." do) will be discarded at the end of the test. You can inspect the log/test.log ($ tail -f log/test.log) to see that in action. That ensures that tests won't affect each other. Each test starts with the assumption of a clean database.

    Read https://api.rubyonrails.org/v6.1/classes/ActiveRecord/FixtureSet.html for more details