LearnThatStack Ace your next interview
API Design · question
Question 43 of 92

How do you implement API testing strategies?

intermediate Pro
← All API Design questions

The full answer covers the core mechanics, when this pattern wins, and the trade-offs interviewers most often probe. It includes a code sample. Read it once at your own pace, then try to recall the structure from memory before the interview. Pro also includes this question's interview lens - the likely follow-up probes and what you can say in the room.

Full answer + code samples + AI explanations

Unlock to read the complete answer for this premium question.

Guided practice for API Design One question at a time. Answer out loud, get graded, see what you missed.
Related concept

Testing APIs: three different questions

Three suites, three questions: behaviour, compatibility, capacity. The contract test is the one only an API needs.

For this question · all 3 chapters

1/3 Each layer catches a different bug
testingEach layer catches a different bugand the layer below cannottime to know3 daysfound bya customerproductionfound at 02:14, three days after the deploydeployabove this line, somebody else finds itunit1,200 testsneeds nothing else. Just the functionintegration180 testsneeds the service and a real databasecontractdeletednobody is asking the consumer's questionend to end8 journeysneeds everything, in a browserdoes this function do what it saysformatMoney(0.1 + 0.2) returns "0.30000000000000004"does this endpoint work against the real storeGET /orders?status=open takes 6.4 s over 2 M rowsdoes this still give consumers what they readnothing runs here nowdoes the whole thing work for a personthe login redirect drops the session cookiecheckout-web shows 0.00 on every ordertotal_cents renamed to amount_centsthe layer you skip is the one that charges youthe same rename. 12 seconds in your own build, or three days and somebody else's evening
testingEach layer catches a different bugtime to know3 daysfound bya customerproductioncheckout-web shows 0.00, 02:14deployunit1,200 testsneeds nothing else runningformatMoney(0.1+0.2) = 0.30000000000000004integration180 testsneeds the service and a databaseGET /orders?status=open 6.4 s on 2 M rowscontractdeletednobody is asking the consumer's questionend to end8 journeysneeds everything, in a browserthe login redirect drops the session cookietotal_cents renamed to amount_centsthe layer you skip charges you12 s in your build, or 3 days and a customer

Each layer catches a different bug

  1. Each test layer below the deploy line (the point where code ships) exists to stop a bug before it reaches production. Production is above that line, and it is not a layer you own. The higher the layer, the fewer the tests. There are 1,200 unit tests and 180 integration tests, then 24 contracts and 8 end to end journeys. But each higher layer needs more of the system running before a test means anything.
  2. Speed is the whole reason there are 1,200 unit tests. One of them catches formatMoney(0.1 + 0.2) returning 0.30000000000000004 in 0.3 seconds, on your own machine. The bug is arithmetic inside one function, so the test needs nothing else running. You run these tests before the commit exists and barely notice the wait.
  3. The next bug appears only against a real database. The request GET /orders?status=open takes 6.4 seconds over 2 million rows, because the status column has no index. A unit test with a fake repository never touches an index, so no test below this layer could have seen this problem. The integration test takes 40 seconds because it needs the service and a real database running.
  4. The commit that renamed total_cents to amount_cents also updated the tests in both layers below, so neither layer catches the rename. The contract layer checks that consumers still get what they read. This check catches the rename in 12 seconds, faster than the integration layer. So a layer is high in this pyramid because it has few tests, not because its tests are slow.
  5. The login redirect drops the session cookie. This bug is in the path between services, not inside any one service, so no lower layer of tests can see the bug. End to end tests take nine minutes in a browser and fail for reasons that have nothing to do with your code. So there are 8 end to end tests, not 800.
  6. Skipping a test layer costs you, because the bugs that layer would catch reach production. Now delete the contract test layer and run the same rename again. The rename ships, so checkout-web shows 0.00 on every order until a customer reports the bug at 02:14, three days later. Your own build would have caught the bug in 12 seconds.
Chapter 1 · step 1 of 6

Each test layer below the deploy line (the point where code ships) exists to stop a bug before it reaches production. Production is above that line, and it is not a layer you own. The higher the layer, the fewer the tests. There are 1,200 unit tests and 180 integration tests, then 24 contracts and 8 end to end journeys. But each higher layer needs more of the system running before a test means anything.

1 of 6 Use ← → or swipe See the concept

© LearnThatStack - diagrams may not be republished without permission.

Want a quick review of the fundamentals? See the API Design cheatsheet.

← Back to all API Design questions
Pro · $10/mo

74 of 92 API Design answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime