Labs ICT
Pro Login

The Testing Pyramid

Unit, integration, and end-to-end tests.

The Testing Pyramid

Have you ever heard someone talk about the testing pyramid and wondered what that even means? I definitely did when I started. It sounds all fancy and architectural, but it is actually pretty simple once someone explains it like a friend.

Think of it like building a house. You need a strong foundation before you start decorating the living room. The testing pyramid is the same idea. You build your testing strategy from the bottom up.

The Three Layers

The pyramid has three layers. At the bottom, you have unit tests. In the middle, integration tests. At the top, end-to-end tests. Each layer tests something different, and you need way more of the bottom layer than the top.


// Unit Test - tests one function in isolation
function calculateTotal(price, quantity) {
  return price * quantity;
}

test('calculates total price', () => {
  expect(calculateTotal(10, 3)).toBe(30);
});

// Integration Test - tests multiple units working together
function addToCart(product, cart) {
  cart.items.push(product);
  cart.total = cart.items.reduce((sum, item) => sum + item.price, 0);
  return cart;
}

test('adds product and updates cart total', () => {
  const cart = { items: [], total: 0 };
  const product = { name: 'Shirt', price: 25 };
  const result = addToCart(product, cart);
  expect(result.items.length).toBe(1);
  expect(result.total).toBe(25);
});
    

See the difference? The unit test checks one tiny function. The integration test checks if two pieces work together. Both matter, but you want way more unit tests.

Why Unit Tests Sit at the Bottom

Unit tests are fast. They run in milliseconds. They are cheap to write. They are easy to maintain. When one fails, you know exactly which function broke. That is why you want a lot of them.

Integration tests are slower. They test how pieces connect. End-to-end tests are the slowest. They test the whole application from start to finish. You need fewer of those because they take longer and are harder to debug when they fail.

The Ratio Rule

A good rule of thumb is something like 70 percent unit tests, 20 percent integration tests, and 10 percent end-to-end tests. That gives you a solid foundation without spending all day waiting for tests to finish.

Try it Yourself →

Key Takeaways

  • The testing pyramid has three layers: unit, integration, and end-to-end
  • Unit tests are fast, cheap, and should make up most of your tests
  • Integration tests check how pieces work together
  • End-to-end tests are slow but verify the whole user experience
  • Aim for roughly 70/20/10 ratio across the layers