Sobes.tech
Junior — Middle

Explain the principle of Test Driven Development and its main features.

sobes.tech AI

Answer from AI

Test Driven Development (TDD) is an approach to software development where tests are written before the main code.

Key features of TDD:

  • First, a test is written that describes the required behavior or functionality.
  • Then, the minimal code is written to pass this test.
  • After that, the code is refactored while keeping the tests passing.

The TDD cycle is often described as "Red-Green-Refactor":

  1. Red — write a test that fails.
  2. Green — write code to pass the test.
  3. Refactor — improve the code without changing its behavior.

This approach helps create more reliable and maintainable code, reduces bugs, and improves design.

Example in Swift for iOS:

func testAddition() {
    let result = add(2, 3)
    XCTAssertEqual(result, 5)
}

func add(_ a: Int, _ b: Int) -> Int {
    return a + b
}