Running two tests simultaneously in RSpec / Rack::Test / Sinatra

1.2k Views Asked by At

Given the example sinatra app

post '/1' do
  sleep(1) until @2
  0
end

post '/2' do
  @2 = true
  0
end

and the example RSpec test

describe 'test' do

  it 'does /1' do
    post '/1'
    expect(last_response.body) to eq?(0)
  end

  it 'does /2'
    post '/2'
    expect(last_response.body) to eq?(0)
  end

end

The first test (it does /1) will hang, waiting for /2 to be called.

Is it possible to tell RSpec to not wait for the outcome of test #1 to complete before beginning test #2? A.K.A, are asynchronous tests possible in RSpec?

1

There are 1 best solutions below

0
joelparkerhenderson On

Yes. An easy way is to use the parallel tests gem:

Similar gems suitable for a CI server:

These gems are all about spreading tests across CPUs. What your code is even better suited for is concurrent tests.

If you're open to using Minitest (which is awesome) then there's excellent concurrency. "Minitest gives you the ability to denote that some or all of your test cases are able to run concurrently, and as pointed out in the source comments, that can only mean one thing: that you rule and your tests are awesome."

See this blog that explains it in detail: