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

Software Compatibility Best Practices

I spoke to some of the most talented and experienced software developers. I have created a guide that is filled with valuable insights and actionable ideas to boost developer productivity.

You will gain a better understanding of what's working well for other developers and how they address the software compatibility problems.

Get the Guide Now