Skip to content

Instantly share code, notes, and snippets.

@yasinishyn
Last active December 28, 2015 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yasinishyn/b09a8d84a21bed5106f0 to your computer and use it in GitHub Desktop.
Save yasinishyn/b09a8d84a21bed5106f0 to your computer and use it in GitHub Desktop.
Minitest tutorial

Minitest

https://github.com/seattlerb/minitest

Now when we have calc class with all its functionality in place, we can write some verifications tests (test, that are written after the code is implemented) using Minitest gem.

Configure

Add minitest gem into your Gemfile

# Gemfile
source 'https://rubygems.org'

## Test runners
gem "test-unit-runner-tap"

## Test Frameworks
# Ruby supplies test unit by default.
# gem "test-unit"
gem "minitest"

1. Create new spec file

Create a new file in the root of your directory.

$ touch minitest_calc_test_unit_format_spec.rb

I'll tell you why I named it test_unit_format in a minute.

Place the necessary code into this file, and run the tests.

# minitest_calc_test_unit_format_spec.rb

require "minitest/autorun"
require_relative "calc"

class CalcTest < Minitest::Test
  def setup
    @calc = Calc.new
  end

  # Negative Tests
  def test_return_zore_if_no_arguments_supplied
    assert_equal @calc.make(),  0
  end

  def test_return_zore_supplied_argument_is_not_a_string
    assert_equal @calc.make(10), 0
  end

  # Positive Tests
  def test_add
    assert_equal @calc.make("2 + 2"), 4
  end

  def test_multiply
    assert_equal @calc.make("8 * 2"), 16
  end

  def test_subtract
    assert_equal @calc.make("2 - 2"), 0
  end

  def test_divide
    assert_equal @calc.make("8 / 2"), 4
  end
end
$ ruby -Ilib:test minitest_calc_test_unit_format_spec.rb
# Running:

......

Finished in 0.001139s, 5266.1097 runs/s, 5266.1097 assertions/s.

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

Notice that the output is very similar to what Rspec output might look like.

The first thing you might notice, is that tests are more DRY. Ve removed duplication from each method by simply adding before hook

def setup
  @calc = Calc.new
end

Spec format

The nice thing about minitest is, that it allows you to write tests using spec DSL that is very similar to Rspec DSL.

$ touch minitest_calc_spec_format_spec.rb
# minitest_calc_spec.rb

require "minitest/autorun"
require_relative "calc"

describe Calc do
  before do
    @calc = Calc.new
  end

  describe "Negative Tests" do
    it "test return zore if no arguments supplied" do
      assert_equal @calc.make(),  0
    end

    it "test return zore supplied argument is not a string" do
      assert_equal @calc.make(10), 0
    end
  end

  describe "Positive Tests" do
    it "test add" do
      assert_equal @calc.make("2 + 2"), 4
    end

    it "test multiply" do
      assert_equal @calc.make("8 * 2"), 16
    end

    it "test subtract" do
      assert_equal @calc.make("2 - 2"), 0
    end

    it "test divide" do
      assert_equal @calc.make("8 / 2"), 4
    end
  end
end

Execute specs, and verify that we are still green.

$ ruby -Ilib:test minitest_calc_spec_format_spec.rb
# Running:

......

Finished in 0.001436s, 4178.9860 runs/s, 4178.9860 assertions/s.

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

Conclusion

As you can see, both frameworks are quite powerful. Minitest is becoming the tool of choice of developers that previously tend to use Test::Unit. Also Ruby On Rails for it's new version 5 will add missing test runner for minitest specs.

My advice is to select whichever you like more. But before that, lets take a look, at the most popular ruby test framework Rspec.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment