Assertions and Behavior Verification Questions
Choosing what to assert and how to verify behavior meaningfully. Covers assertion strategy, verifying observable behavior over implementation detail, avoiding weak or over-specified assertions, and using assertion libraries effectively. Emphasizes assertions that fail for the right reasons and give a clear diagnosis.
Your repository has a test suite that takes hours to run. Design an optimization plan including test selection strategies (change-based), dependency-aware test impact analysis, parallelization across workers, caching test results/artifacts, splitting tests by risk, and incremental runs for pull requests. Discuss trade-offs and rollout strategy.
How would you test time-dependent behavior? For JavaScript code that uses setTimeout to delay a callback, show how to write deterministic tests using fake timers (for example with Jest or sinon). Include an example of advancing the fake clock and asserting that callbacks have been invoked.
When comparing floating-point values in unit tests, what assertion strategies should you use to avoid flaky false negatives? Provide examples for Java (JUnit assertEquals with delta), Python (pytest.approx), and JavaScript (toBeCloseTo or Number.EPSILON), and explain relative vs absolute tolerances.
You have a failing test that compares two large JSON objects. Describe how you would write assertion messages and structure the test so the cause of the failure is obvious. Show an example using pytest or JUnit style assertions and list approaches to minimize noise in diffs (filtering, normalized ordering, highlighting key fields).
You have a large monorepo and a failing test that only fails on a specific CI image. Describe a process to create a minimal reproducible example: isolating environment differences, narrowing dependencies, reducing the code to a small testcase, and documenting steps so others can reproduce locally or in a disposable CI environment.
Unlock Full Question Bank
Get access to all 41 Assertions and Behavior Verification interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.