Sobes.tech
Junior — Middle

What types of tests do you usually write to verify individual code components?

sobes.tech AI

Answer from AI

To test individual code components, the following types of tests are usually written:

  • Unit tests: check the correctness of individual functions or methods in isolation.
  • Integration tests: verify the interaction of multiple components together.

For unit tests in Python, frameworks like unittest or pytest are often used. Here's an example of a simple unit test:

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)

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

Unit tests help quickly identify errors at the level of individual components.