Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run MiniTest::Unit tests in ordered sequence?

MiniTest runs my test cases in parallel. Is there a way to force running test cases in sequence?

def test_1
end

def test_2
end

How can I force test_1 running before test_2?

like image 766
biluochun2010 Avatar asked Oct 09 '14 14:10

biluochun2010


People also ask

How to run Rails test cases?

We can run all of our tests at once by using the bin/rails test command. Or we can run a single test file by passing the bin/rails test command the filename containing the test cases. This will run all test methods from the test case.

What is unit testing in Rails?

They make sure that a section of an application, or a “unit”, is behaving as intended. In a Rails context, unit tests are what you use to test your models. Although it is possible in Rails to run all tests simultaneously, each unit test case should be tested independently to isolate issues that may arise.


2 Answers

You can use i_suck_and_my_tests_are_order_dependent!() class method.

class MyTest < MiniTest::Unit::TestCase
  i_suck_and_my_tests_are_order_dependent!   # <----

  def test_1
    p 1
  end

  def test_2
    p 2
  end
end

But as the name suggest, it's not a good idea to make the tests to be dependant on orders.

like image 127
falsetru Avatar answered Oct 03 '22 09:10

falsetru


Make Your Tests Order-Dependent with MiniTest

If you want to force ordered tests, despite the brittleness of doing so, you can invoke Minitest::Test#i_suck_and_my_tests_are_order_dependent!. The documentation says:

Call this at the top of your tests when you absolutely positively need to have ordered tests. In doing so, you're admitting that you suck and your tests are weak.

Test cases should really be independent. Order-dependent tests are a code smell, and the opinionated name of the MiniTest method makes it quite clear that the MiniTest authors think you need to be doing something else with your code.

State should be defined in setup and teardown blocks, rather than in test ordering. YMMV.

like image 45
Todd A. Jacobs Avatar answered Oct 04 '22 09:10

Todd A. Jacobs