Here, I will explain the Python test using ʻunit test`.
The test is written as follows so that it corresponds to the function to be dealt with.
import unittest
def average(num1, num2):
return (num1 + num2) / 2
class AverageTests(unittest.TestCase):
def test_average(self):
actual = average(1, 2)
expected = 1.5
self.assertEqual(actual, expected)
For the function ʻaverage, a class called ʻAverageTests
is prepared, and test_average
is written as a method in this class.
As you can see, the function for testing is often test_function name to be tested
.
As in the example above, the ʻassert method is used when making test decisions. Some of the most commonly used ʻassert
methods are:
--assertEqual (actual, expected): Whether actual and expected are equal
--assertNotEqual (actual, expected): Whether actual and expected are not equal
--assertTrue (bool): Whether bool is True
--assertFalse (bool): Whether bool is False
--assertGreater (num1, num2): Whether num1 is greater than num2
--assertGreaterEqual (num1, num2): Whether num1 is num2 or higher
--assertLess (num1, num2): Whether num1 is less than num2
--assertLessEqual (num1, num2): Whether num1 is less than or equal to num2
--assertIn (value, values): Whether value is included in values
Here, I explained how to write a Python test using ʻunit test`. I would like to improve development efficiency by putting tests on my side.
Recommended Posts