Unit testing is a fundamental practice in software development that ensures individual components of your code work as expected. In Ruby, Minitest is a popular testing library that provides a simple and efficient way to write and run tests.

What is Minitest?

Minitest is a testing suite that comes pre-installed with Ruby. It supports a variety of testing styles, including unit tests, specs, and mocks. Minitest is lightweight and easy to use, making it a great choice for both beginners and experienced developers.

Setting Up Minitest

Before you start writing tests with Minitest, ensure you have it installed. Since Minitest comes bundled with Ruby, you typically don't need to install it separately. However, if you need to update or install it, you can do so using the following command:

gem install minitest

Writing Your First Test

Let's start by writing a simple test for a basic Ruby class. Consider the following Calculator class:

class Calculator
  def add(a, b)
    a + b
  end

  def subtract(a, b)
    a - b
  end
end

To test this class using Minitest, create a new file named test_calculator.rb:

require 'minitest/autorun'
require_relative 'calculator'

class TestCalculator < Minitest::Test
  def setup
    @calculator = Calculator.new
  end

  def test_add
    assert_equal 5, @calculator.add(2, 3)
  end

  def test_subtract
    assert_equal 1, @calculator.subtract(3, 2)
  end
end

Explanation

  • require 'minitest/autorun': This line includes the Minitest library and automatically runs the tests when the file is executed.
  • require_relative 'calculator': This line includes the Calculator class file.
  • class TestCalculator < Minitest::Test: This defines a new test class that inherits from Minitest::Test.
  • setup method: This method runs before each test, initializing a new Calculator object.
  • test_add and test_subtract methods: These methods contain the actual tests. The assert_equal method checks if the expected value matches the actual value returned by the method being tested.

Running the Tests

To run the tests, simply execute the test file from the command line:

ruby test_calculator.rb

You should see output indicating whether the tests passed or failed.

Common Assertions

Minitest provides several assertion methods to validate your code. Here are some commonly used assertions:

Assertion Method Description
assert Asserts that the expression is true
assert_equal Asserts that two values are equal
assert_nil Asserts that the value is nil
assert_raises Asserts that a specific exception is raised
refute Asserts that the expression is false
refute_equal Asserts that two values are not equal

Example

def test_divide_by_zero
  assert_raises(ZeroDivisionError) { @calculator.divide(1, 0) }
end

Practical Exercise

Exercise

  1. Create a StringManipulator class with the following methods:

    • reverse_string(str): Reverses the given string.
    • upcase_string(str): Converts the given string to uppercase.
  2. Write tests for the StringManipulator class using Minitest.

Solution

StringManipulator class:

class StringManipulator
  def reverse_string(str)
    str.reverse
  end

  def upcase_string(str)
    str.upcase
  end
end

Test file:

require 'minitest/autorun'
require_relative 'string_manipulator'

class TestStringManipulator < Minitest::Test
  def setup
    @manipulator = StringManipulator.new
  end

  def test_reverse_string
    assert_equal 'olleh', @manipulator.reverse_string('hello')
  end

  def test_upcase_string
    assert_equal 'HELLO', @manipulator.upcase_string('hello')
  end
end

Conclusion

In this section, you learned how to set up and use Minitest for unit testing in Ruby. You wrote and ran tests for a simple class, explored common assertion methods, and completed a practical exercise to reinforce your understanding. Unit testing is a crucial skill for ensuring the reliability and maintainability of your code, and Minitest provides a straightforward way to get started. In the next section, we will delve into Behavior-Driven Development (BDD) with RSpec, another powerful testing framework in Ruby.

© Copyright 2024. All rights reserved