Try Ruby Minitest

Purpose

Output about Rails default testing frame.

Test code template

The template of the test code using Minitest is as follows.

sample_test.rb


require 'minitest/autorun'

class SampleTest < Minitest::Test
  def test_sample
    assert_equal 5, 'Hello'.length
  end
end

-Call the library required for Minitest on the first line. -Make the SampleTest class inherit the Minitest :: Test class. -Define the test method to be executed in the SampleTest class (test_sample) It can be created in the flow.

assert_equal is a validation method, the first argument is the expected value and the second argument is the validation value. Therefore, test_sample method, "'Hello' number of characters in the string" is testing whether a "five characters". The test passes because the results are equal. test.png -Runs: Number of executed test methods (1 because only test_sample) -Assertions: Number of validation methods executed (assert_equal is used once) -Failures: Number of test methods that failed validation -Errors: Number of test methods that gave an error during verification -Skips: Number of test methods whose execution was skipped by the skip method Represents.

Validation method example

http://docs.seattlerb.org/minitest/Minitest/Assertions.html Let's use some validation methods by referring to the API documentation of Minitest.

hello_test.rb


require 'minitest/autorun'

def hello(name)
  puts "#{name}San, Hello!"
end

class HelloTest < Minitest::Test
  def test_hello
    assert_output("Bob's, Hello!\n") { hello("Bob") }
  end
end

You can use assert_output to test the standard output.

multiple_of_eleven_test.rb


require 'minitest/autorun'

def multiple_of_eleven?(number)
  number % 11 == 0
end

class MultipleOfElevenTest < Minitest::Test
  def test_true
    assert multiple_of_eleven? 121
  end
  def test_false
    refute multiple_of_eleven? 13
  end
end

You can test the authenticity by using the assert and refute methods.

Postscript

Minitest makes it easy to implement tests, so let's take advantage of them. Next time, I want to output Rspec as well.

References

An introduction to Ruby for those who want to become professionals Junichi Ito [Author]

Recommended Posts

Try Ruby Minitest
Try using Cocoa from Ruby
[Programming Encyclopedia] §2 Try using Ruby
[Ruby on Rails] Read try (: [],: key)
Ruby learning 4
[Ruby] Array
Ruby learning 5
Ruby basics
Try HiveRunner
Ruby Review 2
Ruby addition
Try Mockito
Ruby learning 3
Try DbUnit
Ruby setting 2
Ruby problem ⑦
Ruby learning 2
Ruby learning 6
Ruby settings 1
Try Lombok
Ruby learning 1
Ruby Review 1
Try to link Ruby and Java with Dapr
I made blackjack with Ruby (I tried using minitest)
Try using GPS receiver kit with RaspberryPi3 (Ruby)
Try to get redmine API key with ruby