6

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?

falsetru
  • 357,413
  • 63
  • 732
  • 636
biluochun2010
  • 109
  • 1
  • 7
  • 4
    As a professional tester, I can tell you that this is BAD TEST DESIGN. Your tests should be independent and idempotent. Try to structure it where you don't need to do this. – ddavison Oct 09 '14 at 14:32
  • possible duplicate of [How do I effectively force Minitest to run my tests in order?](http://stackoverflow.com/questions/8752654/how-do-i-effectively-force-minitest-to-run-my-tests-in-order) – Todd A. Jacobs Oct 09 '14 at 14:43
  • @sircapsalot - The problem with that viewpoint is: perhaps test_2 doesn't actually depend on test_1, but when looking at the output, each time I run the tests they are printed in a different order. This makes it considerably difficult to debug as each time you have to search through the output to find the test you are currently working on. Ordering the tests lets you tackle one bug then move on to the next, without unnecessary time wasted looking through hundreds of lines of output (stack traces can get pretty long) – vcapra1 Sep 03 '18 at 14:32
  • @vcapra1 https://stackoverflow.com/questions/5285711/is-it-possible-to-run-a-single-test-in-minitest#10659565 – go2null Dec 11 '19 at 23:34

2 Answers2

25

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.

falsetru
  • 357,413
  • 63
  • 732
  • 636
  • 1
    OH MY GOSH! That's AWESOME! I can't believe that's actually a setting! (`...In doing so, you’re admitting that you suck and your tests are weak.`) – ddavison Oct 09 '14 at 14:35
3

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.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199