Testing Fundamentals

Unit, integration, and e2e testing concepts

testing unit-tests integration e2e

Testing Fundamentals

Learn the essential testing strategies that ensure code quality and reliability in software development.

Why Test?

  • Catch bugs early - Before they reach production
  • Enable refactoring - Change code with confidence
  • Document behavior - Tests serve as living documentation
  • Improve design - Testable code is often better designed
  • Types of Testing

    Unit Tests

    Test individual functions or components in isolation.

    javascript

    // Example unit test

    test('calculateTotal should add tax to subtotal', () => {

    const result = calculateTotal(100, 0.08);

    expect(result).toBe(108);

    });

    Characteristics:

  • Fast execution
  • No external dependencies
  • High test coverage
  • Integration Tests

    Test how different parts of your system work together.

    javascript

    // Example integration test

    test('user can create and retrieve a post', async () => {

    const post = await createPost({ title: 'Test', content: 'Content' });

    const retrieved = await getPost(post.id);

    expect(retrieved.title).toBe('Test');

    });

    Characteristics:

  • Test component interactions
  • May involve databases or APIs
  • Slower than unit tests
  • End-to-End (E2E) Tests

    Test complete user workflows from start to finish.

    javascript

    // Example E2E test with Playwright

    test('user can complete checkout process', async ({ page }) => {

    await page.goto('/products');

    await page.click('[data-testid="add-to-cart"]');

    await page.click('[data-testid="checkout"]');

    await expect(page.locator('.success-message')).toBeVisible();

    });

    Characteristics:

  • Test real user scenarios
  • Use actual browser/app
  • Slowest but most comprehensive
  • Testing Pyramid

    /\

    /E2E\ <- Few, slow, expensive

    /____\

    / \

    /INTEGR.\ <- Some, medium speed

    /_________\

    / \

    / UNIT \ <- Many, fast, cheap

    /____________\

    Test-Driven Development (TDD)

  • Red - Write a failing test
  • Green - Write minimal code to pass
  • Refactor - Improve code while keeping tests green
  • Popular Testing Frameworks

    JavaScript/TypeScript

  • Jest - Feature-rich testing framework
  • Vitest - Fast Vite-native test runner
  • Playwright - Modern E2E testing
  • Cypress - Developer-friendly E2E testing
  • Other Languages

  • Python - pytest, unittest
  • Java - JUnit, TestNG
  • C# - xUnit, NUnit

Best Practices

  • Write tests first - TDD or at least test-alongside
  • Keep tests simple - One assertion per test when possible
  • Use descriptive names - Tests should read like specifications
  • Mock external dependencies - Control test environment
  • Maintain test coverage - Aim for 80%+ but focus on critical paths
  • Getting Started

  • Start with unit tests for core business logic
  • Add integration tests for critical workflows
  • Include E2E tests for main user journeys
  • Set up continuous testing in your CI pipeline