Sinatra Basics : Writing a Test using Unit Test framework


Learn how to write a test using test unit framework for a Sinatra application.


Step 1

Create a new project folder to keep all the files.

Step 2

Add rack-test gem to Gemfile

gem "rack-test", require: "rack/test"

the entire Gemfile looks like this:

source ''

gem 'shotgun'
gem 'sinatra'
gem "rack-test", require: "rack/test"
gem 'test-unit'

Step 3

Install the gems.


This installed test-unit gem version 3.2 and rack-test gem version 0.6.3 on my system.

Step 4

Create a test folder inside the project folder and create sample_test.rb in it.

ENV['RACK_ENV'] = 'test'

require 'test/unit'
require 'rack/test'

require File.expand_path '../sample.rb'

class SampleTest < Test::Unit::TestCase
  include Rack::Test::Methods

  def app

  def test_sample
    get '/'

    assert last_response.ok?
    assert_equal 'hi', last_response.body


Step 5

Run the test:

bundle exec ruby sample_test.rb

Step 6

In the project folder, create sample.rb:

require 'sinatra'

get '/' do


In this article, you learned how to write a test using TestUnit testing framework to test Sinatra application.

Related Articles

Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.