Testing Pyramid
Testing Pyramid
Balance between different test types.
The Pyramid
/
/ E2E (10%) \
/ Slow, Expensive \
/--------------------\
/ Integration (20%) \
/ Moderate Speed/Cost \
/--------------------------\
/ Unit Tests (70%) \
/ Fast, Cheap, Many \
/--------------------------------\
Test Distribution
| Type | % | Speed | Cost | Confidence |
|---|---|---|---|---|
| Unit | 70% | Fast | Low | Low |
| Integration | 20% | Medium | Medium | Medium |
| E2E | 10% | Slow | High | High |
Anti-Patterns
- Ice Cream Cone: More E2E than unit tests
- Funnel: All tests are E2E
- Hourglass: No integration tests
When to Use Each
Unit Tests:
- Pure functions
- Utility functions
- Complex logic
- Business rules
Integration Tests:
- Component interactions
- API integrations
- State management
- Form submissions
E2E Tests:
- Critical user journeys
- Checkout flows
- Authentication
- Cross-browser testing
What to Test First
What to Test First
Prioritize tests based on value.
Priority Matrix
Critical business logic (highest priority)
- Payment processing
- Authentication
- Data validation
Complex components
- Forms with validation
- Interactive widgets
- Data displays
Edge cases
- Error handling
- Empty states
- Loading states
UI details (lowest priority)
- Styling
- Animations
- Responsive design
Testing Checklist
## Must Test
- [ ] Authentication flows
- [ ] Payment processing
- [ ] Form validation
- [ ] Error handling
- [ ] Data CRUD operations
## Should Test
- [ ] Complex business logic
- [ ] Component interactions
- [ ] API integrations
- [ ] State management
## Nice to Test
- [ ] UI edge cases
- [ ] Responsive behavior
- [ ] Accessibility
TDD vs BDD
TDD vs BDD
TDD (Test-Driven Development)
1. Red: Write failing test
2. Green: Write minimal code to pass
3. Refactor: Improve code quality
// TDD Example
// 1. Write test first
it('should calculate total price', () => {
expect(calculateTotal([10, 20, 30])).toBe(60);
});
// 2. Write implementation
function calculateTotal(prices) {
return prices.reduce((sum, price) => sum + price, 0);
}
BDD (Behavior-Driven Development)
Given - Context
When - Action
Then - Expected outcome
// BDD Example
describe('Shopping Cart', () => {
it('should calculate total when items are added', () => {
// Given
const cart = new ShoppingCart();
// When
cart.addItem({ price: 10 });
cart.addItem({ price: 20 });
// Then
expect(cart.total).toBe(30);
});
});
Comparison
| Aspect | TDD | BDD |
|---|---|---|
| Focus | Code behavior | Business behavior |
| Language | Technical | Domain-specific |
| Collaboration | Developers | Everyone |
| Documentation | Code | Living docs |
When to Use
Use TDD when:
- Building complex algorithms
- Working on infrastructure
- Need precise code coverage
Use BDD when:
- Cross-functional teams
- Complex business rules
- Need shared understanding
Tools
- TDD: Jest, Vitest, Mocha
- BDD: Cucumber, Jest with describe/it
Practice Problems
Create a reusable React component implementing Test Strategy. Include proper state management and accessibility.
Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where neededWrite unit and integration tests for Test Strategy using React Testing Library.
Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility testsOptimize Test Strategy for performance. Consider memoization, code splitting, and bundle size.
Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysisQuiz
1. What is the ideal test distribution?
2. What should you test first?
3. What is the primary purpose of Test Strategy?
4. What is a common mistake when implementing Test Strategy?
Flashcards
Question
What is the testing pyramid?
Click to reveal answer
Answer
A model suggesting 70% unit, 20% integration, 10% E2E tests for optimal balance.
Question
What is the ice cream cone anti-pattern?
Click to reveal answer
Answer
Having more E2E tests than unit tests, making tests slow and expensive.
Question
What is the Red-Green-Refactor cycle?
Click to reveal answer
Answer
TDD cycle: write failing test (Red), make it pass (Green), improve code (Refactor).
Question
What is the Given/When/Then format?
Click to reveal answer
Answer
BDD format: Given context, When action, Then expected outcome.
Question
What is Test Strategy?
Click to reveal answer
Answer
Test Strategy is a key concept in frontend development.
Revision Notes
Key Takeaways
- 1.Follow the testing pyramid: 70% unit, 20% integration, 10% E2E
- 2.Test critical business logic first
- 3.TDD focuses on code behavior; BDD on business behavior
- 4.Avoid anti-patterns like ice cream cone
- 5.Balance speed, cost, and confidence
Interview Tips
- •Explain the testing pyramid and its distribution
- •Discuss when to use TDD vs BDD
- •Know how to prioritize what to test
Cheat Sheet
Test Strategy Cheat Sheet
Testing Pyramid
- 70% Unit (fast, cheap)
- 20% Integration (moderate)
- 10% E2E (slow, expensive)
Priority
- Critical business logic
- Complex components
- Edge cases
- UI details
TDD
- Red: Write failing test
- Green: Make it pass
- Refactor: Improve code
BDD
- Given: Context
- When: Action
- Then: Outcome