Junior — Middle
Have you dealt with writing and executing unit tests in your projects?
sobes.tech AI
Answer from AI
Yes, in Python projects I have written and executed unit tests using tools such as unittest and pytest.
Unit tests allow you to verify the correctness of individual functions or classes.
Here's a simple example of a test using unittest:
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()
This approach helps identify errors early in development and maintain code quality.