What is unit testing?

Answer

Unit testing involves testing individual units (functions, methods, classes) of code in isolation to verify they behave correctly. "Unit" is the smallest testable piece of code. Key characteristics: Isolated: dependencies are replaced with test doubles (mocks/stubs). Fast: run in milliseconds. Deterministic: same input always produces same result. Independent: no dependency between tests. Unit tests are the foundation of the testing pyramid — the most numerous, fastest, and cheapest to write. Example in JavaScript (Jest): test("add returns sum", () => { expect(add(2, 3)).toBe(5); });. Benefits: (1) Fast feedback during development. (2) Regression detection. (3) Forces good design (testable code is often well-designed). (4) Documentation of intended behavior. (5) Enables safe refactoring. Frameworks: JUnit (Java), NUnit/xUnit (C#), pytest (Python), Jest/Vitest (JavaScript), PHPUnit (PHP).