Sobes.tech
Junior — Middle

How can you implement testing of individual components of your code to verify their correctness?

sobes.tech AI

Answer from AI

To test individual components of code in Python, the unittest module or third-party libraries like pytest are usually used. The main idea is to write tests that verify the correctness of functions or classes with different input data.

Example using unittest:

import unittest

def add(a, b):
    return a + b

class TestAddFunction(unittest.TestCase):
    def test_positive_numbers(self):
        self.assertEqual(add(2, 3), 5)

    def test_negative_numbers(self):
        self.assertEqual(add(-1, -1), -2)

if __name__ == '__main__':
    unittest.main()

This approach allows automating checks, quickly identifying errors when code changes, and maintaining project quality.