0

I'm trying to implement shoulda for test automation. I tried this code:

require 'test/unit'
require 'shoulda'
require 'shoulda-context'

class TestClass < Test::Unit::TestCase
  context 'Languages' do
    should 'Ruby' do
      puts 'Ruby'
    end

    should 'Java' do
      puts 'Java'
    end

    should 'Python' do
      puts 'Python'
    end
  end
end

When I execute that code, it outputs according to alphabetical order of shoulda test methods:

Java
Python
Ruby

Actually, I want the output in the order I wrote the methods:

Ruby
Java
Python

To do that, what will I have to use in my code?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Alpha
  • 13,320
  • 27
  • 96
  • 163

1 Answers1

4

Actually it is preferred not to run test in a specific order. It is better to run them in a random order, because that allows you to recognize if there are tests that depend on other tests. If tests only pass in a specific order, it is a indicator that you are doing something wrong.

If you want to do that nevertheless:

class TestClass < Test::Unit::TestCase
  context 'Languages' do
    self.test_order = :defined
    ...

See: http://test-unit.rubyforge.org/test-unit/en/Test/Unit/TestCase.html#test_order=-class_method

spickermann
  • 100,941
  • 9
  • 101
  • 131
  • I'd use "is preferred not to run". :) – Sergio Tulentsev Sep 21 '13 at 10:04
  • Thanks spickermann. I tried your solution. But it's not working. I put test_order = :defined in my above code still should test methods are executed in alphabetical order. – Alpha Sep 21 '13 at 10:29
  • My fault, please try `self.test_order = :defined`. – spickermann Sep 21 '13 at 10:40
  • Now, it throws ``': undefined method `test_order=' for TestClass:Class (NoMethodError)` Now, I've another question, this ordering is not specific to `should` library. Here, it's related to `testunit`. Am I right? – Alpha Sep 21 '13 at 10:43
  • What version of ruby do you use? `test_order` does not exist in 1.8.x – spickermann Sep 21 '13 at 10:54
  • `ruby 1.9.3p392 (2013-02-22) [i386-mingw32]` – Alpha Sep 21 '13 at 11:02
  • Ruby 1.9.3 has only a hard coded order (see: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/test/unit/rdoc/Test/Unit/TestCase.html), no setter. I am afraid it is not possible to set the order anymore. For a good reason I guess. – spickermann Sep 21 '13 at 11:41
  • I do not know how important it is for you to run the tests in a defined order. And I do not know if RSpec is an option for you. But RSpec definitely accepts a `order` argument: https://www.relishapp.com/rspec/rspec-core/v/2-11/docs/command-line/order-new-in-rspec-core-2-8 – spickermann Sep 21 '13 at 11:45
  • spickermann, thanks a lot for your help! Actually, this is not important for me. I came across this issue while I started learning `shoula` I'll try with RSpec. – Alpha Sep 21 '13 at 11:56