Create assert_equal to make it easy to write tests

!macOS-11.1!ruby-2.7.2p137

Preface (Introduction)

In Test Driven Development (TDD), it is basic to repeat simple tests over and over again.

In this article, I'll create a function assert \ _equal that checks if an expression or value is the same as expected, and use it to create simple test code.

Assert

Equal

First, make sure that the values ​​of expected and result are equal.

assert_equal.rb


def assert_equal(expected, result)
  return expected == result
end

p assert_equal(1, 1)
> ruby assert_equal.rb
-> true

Then use the colorize library to color the output for visual clarity.

assert_equal.rb


require 'colorize'

def assert_equal(expected, result)
  if expected == result
    puts 'true'.green
  else
    puts 'false'.red
  end
end

assert_equal(1, 1)
assert_equal(1, 2)

The result should be green true for the first and red false for the second.

Make a few improvements.

--Output the expected and result values ​​of the arguments --Changed the processing of assert \ _equal to the following - true: print "succeeded in assert_equal.\n".green - false: print "failed in assert_equal.\n".red

assert_equal.rb


require 'colorize'

def puts_vals(expected, result)
  puts "expected :: #{expected}"
  puts "result   :: #{result}"
end
def assert_equal(expected, result)
  puts_vals(expected, result)
  print  case expected == result
     when true  ; "succeeded in #{__method__}.\n".green
     when false ; "failed in #{__method__}.\n".red
     end
end

assert_equal(1, 1)
assert_equal(1, 2)

The output is

> ruby assert_equal.rb
expected :: 1
result   :: 1
succeeded in assert_equal.
expected :: 1
result   :: 2
failed in assert_equal.

(The color has not changed on Qiita.)

Not Equal

Now create an assert \ _not \ _equal function that returns false if expected and result are equal, and true if they are different.

assert_equal.rb


require 'colorize'

def puts_vals(expected, result)
  puts "expected :: #{expected}"
  puts "result   :: #{result}"
end
def assert_not_equal(expected, result)
  puts_vals(expected, result)
  print expected != result ?
  "succeeded in #{__method__}.\n".green :
    "failed in #{__method__}.\n".red
end
def assert_equal(expected, result)
  puts_vals(expected, result)
  print  case expected == result
     when true  ; "succeeded in #{__method__}.\n".green
     when false ; "failed in #{__method__}.\n".red
     end
end

assert_equal(1, 1)
assert_equal(1, 2)
assert_not_equal(1, 2)
assert_not_equal(1, 1)

The output is

> ruby assert_equal.rb
expected :: 1
result   :: 1
succeeded in assert_equal.
expected :: 1
result   :: 2
failed in assert_equal.
expected :: 1
result   :: 2
succeeded in assert_not_equal.
expected :: 1
result   :: 1
failed in assert_not_equal.

Since this assert \ _equal and assert \ _not \ _equal are convenient, I would like to use them for other code as well. However, as it is, the tests in the code will also be executed, so

assert_equal.rb


if $PROGRAM_NAME == __FILE__
  assert_equal(1, 1)
  assert_equal(1, 2)
  assert_not_equal(1, 2)
  assert_not_equal(1, 1)
end

Change the test part like this. By writing like this, if the file name (FILE) where the code is written matches the file name ($ PROGRAM \ _FILE) where PROGRAM is running, the contents can be executed.

Reference material

Chart type ruby-IV (assert \ _equal)

Recommended Posts

Create assert_equal to make it easy to write tests
Easy to create Processing library
SpringBoot + Redis Easy to make demo
Easy to make Slack Bot in Java
Create a program to post to Slack with GO and make it a container
Easy way to create JSP custom tags
Easy way to create an implementation of java.util.stream.Stream
Write code that is easy to maintain (Part 4)
Easy to make LINE BOT with Java Servlet
Write code that is easy to maintain (Part 3)
A solution that makes it easy to input Procon and Web tests to verify results
How to make Java unit tests (JUnit & Mockito & PowerMock)
[Even beginners can do it! ] How to write Javadoc
Refactor Fat Controller to make it look like DDD
Make it possible to search Japanese sentences with ElasticSearch