The assignment said, "Test the rock-paper-scissors program using test / unit." I heard the test code for the first time, so I will write it as a memo for myself. The operating environment is as follows.
mac Ruby 2.6.5 test/unit 3.3.6
Test the rock-paper-scissors program using "test / unit".
In the first place, the test code is a program to check whether the logic of the written program is working as expected.
test / unit is a test framework for Ruby, and it seems that it was originally installed as standard. Please refer to here for the installation of tesu / unit itself.
As a premise, please note that it differs slightly depending on the ver. The basic syntax is as follows.
require 'test/unit'
class [class name] <Test :: Unit :: TestCase
end
First, require``'test / unit'
to use this framework.
Next, create a class that inherits Test :: Unit :: TestCase
.
In Ruby, by inheriting Test :: Unit :: TestCase
, the class becomes a test executable file.
require 'test/unit'
class [class name] <Test :: Unit :: TestCase
def test_ [method name]
foo = "hoge"
assert_equal("hoge", foo)
end
end
The test process is as above.
Create a method and write the program you want to test there. The rule is that method names start with test_
. In this method, write the check method in the following format. (There are other check methods)
assert_equal (assumed values, variables, methods, etc.)
ʻAssert_equal` makes sure that the value of the variable and the return value of the method match what you expected.
This is the end of the quick tutorial, but there are more methods available by default, so check out the reference.
Then, the solution of the achievement condition is described below. First of all, I will write the rock-paper-scissors program first. (It's not a very smart program, but please forgive me) If you input g, c or p as standard input, the result of rock-paper-scissors is output.
player = gets.chomp
if player == "g"
player = "goo"
elsif player == "c"
player = "choki"
elsif player == "p"
player = "par"
end
com = rand(3)
if com == 0
com = "goo"
elsif com == 1
com = "choki"
else
com = "par"
end
if player == com
res = "draw"
elsif (player == "Goo" && com == "Choki") || (player == "Choki" && com == "Par") || (player == "Par" && com == "Goo")
res = "You win"
else
res = "You lose"
end
puts "You are # {player}, I am # {com}, # {res}."
In this case, we will test whether the correct result is displayed when the values of the two variables player
and com
are decided, so we will test the following part.
if player == com
res = "draw"
elsif (player == "Goo" && com == "Choki") || (player == "Choki" && com == "Par") || (player == "Par" && com == "Goo")
res = "You win"
else
res = "You lose"
end
puts "You are # {player}, I am # {com}, # {res}."
This time, we will summarize the above process of determining victory or defeat in a class.
class Janken
def self.judge(player,com)
if player == com
res = "draw"
elsif (player == "Goo" && com == "Choki") || (player == "Choki" && com == "Par") || (player == "Par" && com == "Goo")
res = "You win"
else
res = "You lose"
end
return "You are # {player}, I am # {com}, # {res}."
end
end
The processing of winning and losing of rock-paper-scissors is summarized in the class. Then, write the test process.
require 'test/unit'
class Test_Janken < Test::Unit::TestCase
def test_janken
assert_equal ('You are goo, I am goo, draw.', Janken.judge ("goo", "goo"))
assert_equal ('You are choki, I am choki, draw.', Janken.judge ("choki", "choki"))
assert_equal ('You are par, I am par, draw.', Janken.judge ("par", "par"))
assert_equal ('You are goo, I am choki, you win.', Janken.judge ("goo", "choki"))
assert_equal ('You are choki, I am par, you win.', Janken.judge ("choki", "par"))
assert_equal ('You are par, I am goo, you win.', Janken.judge ("par", "goo"))
assert_equal ('You are goo, I am par, you lose.', Janken.judge ("goo", "par"))
assert_equal ('You are choki, I am goo, you lose.', Janken.judge ("choki", "goo"))
assert_equal ('You are par, I am choki, you lose.', Janken.judge ("par", "choki"))
end
end
It's like verifying all 9 patterns of rock-paper-scissors wins and losses with ʻassert_equal`. This is combined with the processing of rock-paper-scissors. The following code is the final one.
require 'test/unit'
class Janken
def self.judge(player,com)
if player == com
res = "draw"
elsif (player == "Goo" && com == "Choki") || (player == "Choki" && com == "Par") || (player == "Par" && com == "Goo")
res = "You win"
else
res = "You lose"
end
return "You are # {player}, I am # {com}, # {res}."
end
end
class Test_Janken < Test::Unit::TestCase
def test_janken
assert_equal ('You are goo, I am goo, draw.', Janken.judge ("goo", "goo"))
assert_equal ('You are choki, I am choki, draw.', Janken.judge ("choki", "choki"))
assert_equal ('You are par, I am par, draw.', Janken.judge ("par", "par"))
assert_equal ('You are goo, I am choki, you win.', Janken.judge ("goo", "choki"))
assert_equal ('You are choki, I am par, you win.', Janken.judge ("choki", "par"))
assert_equal ('You are par, I am goo, you win.', Janken.judge ("par", "goo"))
assert_equal ('You are goo, I am par, you lose.', Janken.judge ("goo", "par"))
assert_equal ('You are choki, I am goo, you lose.', Janken.judge ("choki", "goo"))
assert_equal ('You are par, I am choki, you lose.', Janken.judge ("par", "choki"))
end
end
Result is,,
Loaded suite test
Started
.
Finished in 0.000811 seconds.
--------------------------------------------------------------------------------
1 tests, 9 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
--------------------------------------------------------------------------------
1233.05 tests/s, 11097.41 assertions/s
I was able to test without any problems.
test / unit library --doc.ruby-lang.org
How to use ruby's default Unit test-Brain juice portal
Write a test with Test :: Unit -Qiita
Recommended Posts