Programming5 min read10 interview questions

The testing pyramid

How many tests of each kind a healthy project needs.

In one sentenceWrite many small fast tests, fewer medium ones, and only a few slow tests that click through the whole app.

Think of it like thisIt is checking a car: test every part on the bench, test the systems together, and take it for just a few real drives.

The big picture

Tap any step to see where to use it, how to use it and an example result.

Unit tests: the wide base

They test single functions with no network or database, so they run in milliseconds and point right at the broken code. You can have thousands.

Integration tests: the middle

They check that parts work together, for example your code with a real database or an API. They are slower and a little harder to set up, so you write fewer.

End-to-end tests: the tip

They drive a real browser through key journeys such as signing up and paying. They give confidence the whole thing works, but are slow and easily broken by small changes, so keep them for the most important paths.

Common mistakes

  • Having only end-to-end tests, so the suite is slow and flaky.
  • Testing implementation details so every refactor breaks tests.
  • Never running the tests automatically.

Quick recap

  • Many unit tests, some integration tests, few end-to-end tests
  • Fast tests give fast feedback
  • Test behaviour, not internals
  • Run them automatically on every change

Try it yourself

Pick one small function in your own project and write three unit tests for it, including one for a mistake case.

Top 10 interview questions

Ordered from basic to advanced. Try answering out loud first, then open the answer.

  1. What is the testing pyramid?Basic

    A guide for balancing tests: many fast unit tests at the bottom, fewer integration tests in the middle and a small number of slow end-to-end tests at the top.

  2. What is a unit test?Basic

    A test that checks one small piece of code, such as a function, in isolation. It runs in milliseconds and points directly at what broke.

  3. What is an integration test?Basic

    A test that checks several parts working together, such as your code with a real database or another service.

  4. What is an end-to-end test?Basic

    A test that drives the whole system as a user would, for example through a browser from login to checkout. It is realistic but slow and more fragile.

  5. Why is the pyramid shaped that way?Intermediate

    Lower-level tests are faster, cheaper and more stable, so you can have many of them. High-level tests are slow and break more easily, so they are kept for the most important journeys.

  6. What are mocks and stubs?Intermediate

    Fake stand-ins for real dependencies. A stub returns fixed answers; a mock also records how it was called. They keep unit tests fast and predictable.

  7. What is test coverage, and is 100% the goal?Intermediate

    The share of code that tests run. It shows what is untested but not whether tests are good. Chasing 100% leads to weak tests; aim for meaningful coverage of important behaviour.

  8. What is a flaky test and how do you handle it?Intermediate

    A test that sometimes passes and sometimes fails without code changes, often because of timing, shared data or external services. Find and fix the cause, or quarantine it, rather than ignoring it.

  9. What is the "ice cream cone" anti-pattern?Advanced

    Having mostly slow manual or end-to-end tests and few unit tests. Feedback is slow and failures are hard to diagnose. The fix is to push checks down to cheaper levels.

  10. What is test-driven development, and what other test types matter?Advanced

    TDD means writing a failing test first, then the code to pass it, then cleaning up. Beyond the pyramid, also consider contract, performance, security and accessibility tests.

Back to Learn Hub

Guides are simplified on purpose. Tool names and details change, so check the official documentation before relying on a detail.