The API Test Pyramid
Balance unit, integration, and end-to-end tests for reliable HTTP services.
Lesson content
๐ฏ Goal By the end of this lesson, you'll understand: What is the API Test Pyramid? Why do we need it? Types of tests in the pyramid Unit Testing Integration Testing API Testing End-to-End (E2E) Testing Real-world examples Interview questions What is the API Test Pyramid? The API Test Pyramid is a testing strategy that recommends writing more low-level tests and fewer high-level tests . It helps build applications that are: Reliable Faster to test Easier to maintain Less expensive to test Simple Definition: The API Test Pyramid shows how many tests should be written at each testing level. Why Do We Need the Test Pyramid? Without proper testing: Bugs reach production Features break unexpectedly Development slows down Maintenance becomes difficult The Test Pyramid helps catch bugs early. Test Pyramid Diagram โฒ โ End-to-End Tests (Few, Slow, Expensive) โ API / Integration Tests (Some, Medium Speed) โ Unit Tests (Many, Fast, Low Cost) โผ Levels of the Test Pyramid Level Number of Tests Speed Unit Tests Most Fast API / Integration Tests Moderate Medium End-to-End Tests Few Slow 1. Unit Testing Unit tests verify a small piece of code , usually a single function. Example function add ( a , b ) { return a + b ; } Test test ( "adds two numbers" , () => { expect ( add ( 2 , 3 )) . toBe( 5 ); }); Characteristics of Unit Tests Fast Independent Easy to write No database required No API calls 2. Integration Testing Integration tests verify that multiple components work together . Example: Route โ Controller โ Service โ Database The test checks whether these components interact correctly. Integration Example POST /users โ โผ Controller โ โผ Service โ โผ Database If the user is saved successfully, the integration test passes. 3. API Testing API tests verify that the API behaves correctly. They check: Status Codes Response Body Headers Authentication Validation Example GET /users Expected 200 OK Response [ { "id": 1 , "name": "Ajay" } ] API Test Example (Jest + Supertest) Install npm install --save-dev jest supertest Example const request = require ( "supertest" ); const app = require ( "../app" ); test ( "GET /users" , async () => { const response = await request ( app ) . get( "/users" ); expect ( response . status) . toBe( 200 ); }); 4. End-to-End (E2E) Testing E2E tests verify the entire application from the user's perspective. Example: User Login โ โผ Browse Products โ โผ Add to Cart โ โผ Checkout โ โผ Payment Successful Everything works together. Real-Life Example ๐ฆ Imagine a bank. Unit Test Check ATM PIN Validation Integration Test ATM โ โผ Bank Server โ โผ Database API Test POST /login Returns 200 OK End-to-End Test Customer Login Transfer Money Receive Confirmation Entire flow is tested. Comparison Table Feature Unit Integration API E2E Speed Fast Medium Medium Slow Database โ Usually No โ Often Yes โ Usually Yes โ Yes Network โ No Sometimes โ Yes โ Yes Cost Low Medium Medium High Reliability High High High High Quantity Many Some Some Few Test Execution Flow Write Code โ โผ Unit Tests โ โผ Integration Tests โ โผ API Tests โ โผ End-to-End Tests โ โผ Production Example in Node.js Project project/ โ โโโ src/ โโโ tests/ โ โโโ unit/ โ โโโ integration/ โ โโโ api/ โ โโโ e2e/ โ โโโ package.json โโโ app.js Popular Testing Tools Tool Purpose Jest Unit Testing Supertest API Testing Mocha Unit & Integration Testing Chai Assertions Cypress End-to-End Testing Playwright End-to-End Testing Postman Manual API Testing Testing Flow Developer โ โผ Write Code โ โผ Run Unit Tests โ โผ Run Integration Tests โ โผ Run API Tests โ โผ Run E2E Tests โ โผ Deploy Benefits of API Testing โ Faster than UI testing โ Finds backend bugs early โ Validates business logic โ Verifies authentication and authorization โ Easy to automate Real-Life Example ๐ Shopping Website Unit Test Calculate Price โ Integration Test Order + Payment โ API Test POST /orders โ E2E Test Customer Places Order Successfully Interview Question What is the API Test Pyramid? Answer The API Test Pyramid is a testing strategy that recommends writing many fast unit tests, a moderate number of integration and API tests, and only a few end-to-end tests. Which tests should be written the most? Answer Unit Tests Which tests are the slowest? Answer End-to-End Tests Which testing library is commonly used for API testing in Node.js? Answer Supertest Which testing framework is commonly used with Node.js? Answer Jest Best Practices โ Write many Unit Tests. โ Test APIs using Supertest. โ Keep E2E tests limited to critical user journeys. โ Mock external services in Unit Tests. โ Run automated tests in CI/CD pipelines. โ Keep tests independent and repeatable. โ Don't rely only on manual testing. โ Don't write too many slow E2E tests. Key Points โ The Test Pyramid promotes more Unit Tests and fewer E2E Tests. โ Unit Tests are fast and test individual functions. โ Integration Tests verify multiple components working together. โ API Tests validate endpoints, status codes, authentication, and responses. โ E2E Tests verify complete user workflows. โ A balanced testing strategy improves quality while keeping test execution fast. ๐ง Easy Formula Write Code โ โผ Unit Tests (Many) โ โผ Integration Tests (Some) โ โผ API Tests (Some) โ โผ E2E Tests (Few) โ โผ Deploy Unit = Function Integration = Components Together API = Endpoint Testing E2E = Complete User Journey Remember: ๐งช Unit Tests = Test one function ๐ Integration Tests = Test multiple components together ๐ API Tests = Test REST API endpoints ๐ค E2E Tests = Test the complete application flow ๐ Write many Unit Tests, some Integration/API Tests, and only a few E2E Tests for a healthy testing strategy