Search code examples
rspecrspec-rails

How to execute all contexts in the defined order in RSPEC


My _spec structure is something as mentioned below

describe do

context do

end

context do

end

end

How I can ensure that the order of execution of all context will be as I have defined it? Currently all these executes in the random manner. Thanks


Solution

  • rspec will execute tests within a spec file in the defined order if you call it as follows:

    rspec --order defined
    

    You can also tag the example group (the one you specified with describe) with the meta-data order: :defined to do the same thing.

    However, this only works for the test within the file. rspec will execute the files in alphabetical order. This is a current restriction in the program, based on this issue report where the author states: "RSpec does not execute tests in alphabetical order. It loads spec files in alphabetical order and then runs tests in defined order or random order depending on your configuration."

    You execute each file in a single rspec command with a little bash magic, but it may or may not be worth the effort:

    echo "file4 file3 file1 file2" |
      xargs -n1 -t bundle exec rspec --format doc --order defined