Output about Rails default testing frame.
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. -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.
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.
Minitest makes it easy to implement tests, so let's take advantage of them. Next time, I want to output Rspec as well.
An introduction to Ruby for those who want to become professionals Junichi Ito [Author]
Recommended Posts