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