Junior — Middle
Can you give an example of a test that checks individual system components?
sobes.tech AI
Answer from AI
An example of a unit test for checking a single component is a test of a function that calculates the sum of two numbers:
import unittest
def add(a, b):
return a + b
class TestAddFunction(unittest.TestCase):
def test_add_positive_numbers(self):
self.assertEqual(add(2, 3), 5)
def test_add_negative_numbers(self):
self.assertEqual(add(-1, -1), -2)
def test_add_zero(self):
self.assertEqual(add(0, 5), 5)
if __name__ == '__main__':
unittest.main()
This test checks the functionality of a specific function without dependence on other parts of the system, allowing for quick identification of errors in the component's logic.