Junior — Middle
How have you applied test-driven development (TDD) in your projects?
sobes.tech AI
Answer from AI
In projects using TDD, I start by writing an automated test that describes the required behavior of a function or component. This test initially fails because the functionality has not yet been implemented. Then I write the minimal code to pass the test, and afterwards I refactor the code while keeping the tests passing. This approach helps create more reliable and well-tested code, as well as improves understanding of the requirements.
Example in Python 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)
if __name__ == '__main__':
unittest.main()
First, the test test_add_positive_numbers is written, which initially fails, then the add function is implemented.