CALL US ⁄ ⁄   +61 3 9995 0765




Ruby MiniTest, as the name suggests, is a small and fast unit testing framework. Shipped with Ruby 1.9 or higher, MiniTest supports a complete suite of testing capabilities such as TDD, BDD, mocking, and benchmarking. Today, Texo Design - web application development australia will share you topic “RUBY MINITEST WITH ASSERTIONS”

This article aims to demonstrate MiniTest’s main concepts and provide real world examples to get you acquainted quickly. Let’s start with MiniTest::Test, a small and incredibly fast unit. It provides a rich set of assertions to make your tests clean and readable.

Out-of-the-box, Assertions provides a lean and mean a lot of assertions, and programmers find that to be more than sufficient for testing a wide range of code. All assertion methods accept a msg which is printed if the assertion fails.

Writing Your First Test

In this section, we are going to write a simple class and a test for it. This will give us an idea of how MiniTest works.

Create new file call blog.rb

Now time for we create a new test file, the name calls blog_test.rb

First, we need to require ‘minitest/autorun‘ library. Minitest/autorun is the easy and explicit way to run all your tests. And also need to require class ‘blog.rb‘ as Object class for testing.

require ‘minitest/autorun’

require ‘./blog’

 class BlogTest < Minitest::Test

   def setup

     @blog =


   def test_title_is_rails_master

     assert_equal “Rails master Blog”, @blog.title



Run it by following way

ruby blog_test.rb

And here is the result

Run options: –seed 64769

  # Running:


  Finished in 0.002127s, 470.0461 runs/s, 470.0461 assertions/s.

  1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

It passed, is it look easy? Now we are going to find out how to use assertion public class methods supplied by Minitest.

Take a note, I only introduce some popular methods, we can find out all methods here .

assert_empty(obj, msg = nil)

assert_empty receives an object and message (optional parameter) as parameters, it will return Fails if object is not empty.

For instance, add these codes bellow to blog_test.rb and runs the test

def test_assert_is_empty

    # we putted an empty array to assert_empty

    # and of course test will pass

    assert_empty []


Here is results

Run options: –seed 34158

  # Running:


  Finished in 0.001628s, 1228.4265 runs/s, 1842.6398 assertions/s.

  2 runs, 3 assertions, 0 failures, 0 errors, 0 skips

assert_nil(obj, msg = nil)

Like assert_emptyassert_nil receives an object and message (optional parameter) as parameters, it will return Fails if object is not nil. Here is an example.

def test_assert_is_nil

    # we putted a nil object to assert_nil

    # and of course the test will pass

    assert_nil nil


Here is the test results after added test_assert_is_nil function.

Run options: –seed 8499

  # Running:


  Finished in 0.002735s, 1096.7610 runs/s, 1462.3480 assertions/s.

  3 runs, 4 assertions, 0 failures, 0 errors, 0 skips

It was also passed, now if we have a case, I would like to change parameters to assert_empty and assert_nil like that

def test_assert_is_empty

    # we putted an array with 3 values to assert_empty

    assert_empty [1, "Ruby", "This is an array"]


  def test_assert_is_nil

    # we putted a string object to assert_nil

    assert_nil “This is a string”


Lets guess, what will happen if we run the test code?

Run options: –seed 5692

  # Running:


  Finished in 0.001648s, 1819.8539 runs/s, 2426.4718 assertions/s.

  1) Failure:

  BlogTest#test_assert_is_empty [blog_test.rb:16]:

  Expected [1, "Ruby", "This is an array"] to be empty.

  2) Failure:

  BlogTest#test_assert_is_nil [blog_test.rb:22]:

  Expected “This is a string” to be nil.

  3 runs, 4 assertions, 2 failures, 0 errors, 0 skips

Clearly, you can see we got 2 failures after these changes, MiniTest is showing the messages: we expected array [1, “Ruby”, “This is an array”] is empty and “This is a string” to be nil. Look at these messages, we will know which row and test function we were wrong and will be easy to fix it.

assert_equal(exp, act, msg = nil)

assert_equal will return Fails if exp != act. In this case, exp is a value expected and act is actual value. Message is a option.

Now back to Blog object in blog.rb file, we create a new method

# sum of 1 + 2

 def plus_one_with_two

    1 + 2


Now we are going write a test for this method. we expect the return value will be 3.

def test_assert_equal

    # expect 3 value

    assert_equal 3, @blog.plus_one_with_two


Run the code, it passed.

Run options: –seed 65001

  # Running:


  Finished in 0.002963s, 1349.9631 runs/s, 1687.4539 assertions/s.

  4 runs, 5 assertions, 0 failures, 0 errors, 0 skips

assert_in_delta(exp, act, delta = 0.001, msg = nil)

assert_in_delta uses for comparing floats number. It will return fails if exp and act are not within delta of each other. (exp: expected value, act: actual value)

For understanding, we are going to check assertions library code and see how it works

# File lib/minitest/assertions.rb, line 182

  def assert_in_delta exp, act, delta = 0.001, msg = nil

    n = (exp – act).abs

    msg = message(msg) {

      “Expected |#{exp} – #{act}| (#{n}) to be <= #{delta}”


    assert delta >= n, msg


For example, back to Blog object in blog.rb file, we create a new method

def comparing_float_numbers



And then write the test comparing float numbers using assert_in_delta

def test_assert_in_delta

    # the results are within delta of each other.

     assert_in_delta (22.0 / 7.0), @blog.comparing_float_numbers, 0.01


Lets see how it works. First we calculate the absolute value of “(22.0 / 7.0) – Math::PI” and then comparing with delta number “0.01”. If the absolute value is less than or equal delta value. The test will be pass.

Run options: –seed 36812

  # Running:


  Finished in 0.001555s, 3216.2842 runs/s, 3859.5410 assertions/s.

  5 runs, 6 assertions, 0 failures, 0 errors, 0 skips

assert_includes(collection, obj, msg = nil)

assert_includes will return fails unless collection includes object.

For example,

def is_included_object



And the test for assert_includes method

def test_assert_includes

    # it passes if array includes the @blog.is_included_object object

    assert_includes ["Rspec", "MiniTest", "UnitTest"], @blog.is_included_object


It passed

Run options: –seed 9514

  # Running:


  Finished in 0.001740s, 3447.4457 runs/s, 4596.5943 assertions/s.

  6 runs, 8 assertions, 0 failures, 0 errors, 0 skips


One more thing we need to know. When we want to run the test with specified function instead of running whole files, we can do like this way

ruby blog_test.rb -n BlogTest#test_assert_in_denta

The command above will only run the test for function test_assert_in_denta with -n is a option. The result will be:

Run options: -n BlogTest#test_assert_in_denta –seed 15729

  # Running:


  Finished in 0.002058s, 485.8197 runs/s, 485.8197 assertions/s.

  1 runs, 1 assertions, 0 failures, 0 errors, 0 skips


There are a wealth of testing options in the Ruby community. If you’re going to be doing Ruby development, it is important to be familiar with the standard testing library that ships with the language. If you haven’t given MiniTest a try, check it out! You just may like what you see.

Texo Design is one of the renowned names in web application development melbourne. We are the team of well – qualified and experienced staff as we deal with mobile apps, web development and social media campaign. We offer you the best tailor-made solution. Your success is our guarantee as we commit quality, professionalism and communication to our clients. These three pillars will help you in making your formal decision. We transform your ideas into reality with highly innovative technologies and experts mindset. Contact us +61 3 9995 0765 or for more information


Be the first to post on the blog

Leave a Reply


Back to blog