Unit Testing Interview Master
Curated 20+ questions covering unit testing principles, TDD, AAA structure, mocks vs stubs, and code coverage metrics.
Difficulty Filters
Tools & Options
What is Unit Testing, and why is it important?
Unit Testing is a software testing method where the smallest testable parts of an application, called units (typically functions, methods, or classes), are individually and isolatedly scrutinized for proper operation. It is important because it catches bugs early, simplifies debugging, facilitates refactoring, and acts as living documentation.
Explain the F.I.R.S.T. principles of Unit Testing.
The F.I.R.S.T principles are:
1. Fast: Tests should run quickly so developers can run them frequently.
2. Independent: Tests must not depend on each other's execution order or state.
3. Repeatable: Tests must yield the same result in any environment (local, CI).
4. Self-Validating: Tests should have binary outcomes (pass or fail) with no manual interpretation.
5. Timely: Tests should ideally be written before or alongside the code (TDD).
What is the AAA (Arrange-Act-Assert) pattern?
AAA is a structural pattern for writing readable unit tests:
1. Arrange: Set up the test requirements, initialize variables, and mock objects.
2. Act: Execute the unit function/method under test.
3. Assert: Verify that the output or state matches expectations.
// AAA example in Jest
test('adds 1 + 2 to equal 3', () => {
// Arrange
const a = 1;
const b = 2;
// Act
const result = sum(a, b);
// Assert
expect(result).toBe(3);
});Explain the differences between Dummy, Mock, Stub, Spy, and Fake (Test Doubles).
What is Test-Driven Development (TDD), and what is the Red-Green-Refactor cycle?
What is Code Coverage, and what are its different metrics?
Does 100% Code Coverage mean code is bug-free?
What is the Testing Pyramid?
What is the difference between a Unit Test and an Integration Test?
What is Mutation Testing?
What is Mocking, and why should we avoid over-mocking?
Explain Behavior-Driven Development (BDD).
What is regression testing?
What is a test runner vs a test assertion library?
Explain Parameterized Tests.
What is Hermetic testing?
How do you test private methods in a class?
What is a flaky test, and how do you resolve it?
Explain code-first vs test-first development.
How do you handle testing systems that depend on current system time?
Technical Interview Mastery Blueprint
Proven methodologies and frameworks to succeed in technical and architecture rounds.
1. The Engineering STAR Technique
When responding to architectural or situational questions, structure your answer using Situation, Technical Constraint, Action, and Measurable Result. Rather than jumping straight to syntax, articulate the business problem, why certain patterns (e.g. caching, queuing, immutability) were selected over alternatives, and what latency or throughput improvements were achieved.
2. Defending Architectural Trade-Offs
Senior interviewers evaluate how you weigh trade-offs. Always explain why you chose an approach: e.g., memory overhead vs CPU cycle speed, consistency vs availability (CAP theorem), or normalized schemas vs denormalized fast reads. Stating the drawbacks of your own proposal demonstrates maturity and production experience.
3. Clean Code & Edge-Case Proactivity
In live-coding rounds, communicate your thinking aloud before typing. State edge cases explicitly: null or undefined inputs, extreme boundary values, concurrency races, and network timeouts. Writing modular, well-named functions with deterministic unit tests separates junior candidates from staff-level engineers.
4. Antipatterns to Avoid
Avoid reciting memorized definitions without practical context. Never dismiss non-functional requirements such as security (CSRF, injection, token rotation), observability (logging, metrics), and accessibility (ARIA, semantic standards). Always ask clarifying scope questions before writing complex implementations.
Explore Related Tools
Hand-picked utilities and calculators related to this tool.
