The unittest is to try out if the function or program you create works as expected. This time, I will share a rough memorandum as well.
macOS 10.14.6
Below is the code for testing the tashizan
function.
tashizan
is a function that returns the sum of two arguments.
Test.py
import unittest
def tashizan(val1,val2):
ans = val1 + val2
return ans
class Test(unittest.TestCase):
def test_tashizan(self):
actual = tashizan(1, 2)
expected = 3
self.assertEqual(actual, expected)
if __name__ = "__main__":
unittest.main()
The code in Test.py
is explained in order from the top.
Test.py
import unittest
def tashizan(val1,val2):
ans = val1 + val2
return ans
Import unittest and define the function tashizan
.
Test.py
class Test(unittest.TestCase):
def test_tashizan(self):
actual = tashizan(1, 2)
expected = 3
self.assertEqual(actual, expected)
(1) Define the Test
class and inherit the TestCase
class in the ʻunit test. (2) Store the answer in the variable ʻactual
.
③ Store the expected answer in the variable ʻexpected. In this case, 1 + 2 = 3, so 3 is stored in ʻexpected
.
④ The ʻassertEqual method in
TestCase` returns whether the two arguments are equal.
Recommended Posts