πŸ”§ DevOps

Testing

Testing

Code verification. Unit, integration, and E2E tests. TDD is test-first.

Detailed Explanation

Software testing is the process of verifying that code behaves as intended. It was systematized in Glenford Myers's "The Art of Software Testing" in 1979, and it is central to quality assurance in modern software development.

Tests form layers according to their scope. Unit Tests verify individual functions/classes, Integration Tests verify interactions between modules, and E2E Tests (End-to-End) verify the entire system. According to the test pyramid principle, there should be the most Unit tests and the fewest E2E tests.

TDD (Test-Driven Development) is a methodology in which tests are written first and the code is implemented afterward. It proceeds in a Red-Green-Refactor cycle, improving design and preventing regression bugs. BDD (Behavior-Driven Development) tests business requirements in the Given-When-Then format.

In practice, it is important to manage test coverage (typically targeting 80% or higher), test execution time (under 5 minutes in CI is recommended), and flaky tests (tests that fail intermittently). Choose a framework suited to the language and purpose, such as Jest, pytest, JUnit, or Cypress.

Code Example

// Jestλ₯Ό μ‚¬μš©ν•œ ν…ŒμŠ€νŠΈ 예제

// src/utils/calculator.js
export function add(a, b) {
  return a + b;
}

export function divide(a, b) {
  if (b === 0) throw new Error('Division by zero');
  return a / b;
}

// src/utils/calculator.test.js - Unit Test
import { add, divide } from './calculator';

describe('Calculator', () => {
  describe('add', () => {
    it('두 μ–‘μˆ˜λ₯Ό λ”ν•œλ‹€', () => {
      expect(add(2, 3)).toBe(5);
    });

    it('음수λ₯Ό μ²˜λ¦¬ν•œλ‹€', () => {
      expect(add(-1, 1)).toBe(0);
    });
  });

  describe('divide', () => {
    it('두 숫자λ₯Ό λ‚˜λˆˆλ‹€', () => {
      expect(divide(10, 2)).toBe(5);
    });

    it('0으둜 λ‚˜λˆ„λ©΄ μ—λŸ¬λ₯Ό λ˜μ§„λ‹€', () => {
      expect(() => divide(10, 0)).toThrow('Division by zero');
    });
  });
});

// src/services/userService.test.js - Integration Test
import { createUser, getUser } from './userService';
import { db } from '../db';

describe('UserService Integration', () => {
  beforeEach(async () => {
    await db.migrate.latest();
    await db.seed.run();
  });

  afterEach(async () => {
    await db('users').truncate();
  });

  it('μ‚¬μš©μžλ₯Ό μƒμ„±ν•˜κ³  μ‘°νšŒν•  수 μžˆλ‹€', async () => {
    const userData = { name: 'John', email: 'john@example.com' };

    const created = await createUser(userData);
    const retrieved = await getUser(created.id);

    expect(retrieved.name).toBe('John');
    expect(retrieved.email).toBe('john@example.com');
  });
});

// cypress/e2e/login.cy.js - E2E Test
describe('둜그인 ν”Œλ‘œμš°', () => {
  beforeEach(() => {
    cy.visit('/login');
  });

  it('μ˜¬λ°”λ₯Έ 자격 증λͺ…μœΌλ‘œ λ‘œκ·ΈμΈν•  수 μžˆλ‹€', () => {
    cy.get('[data-testid="email"]').type('user@example.com');
    cy.get('[data-testid="password"]').type('password123');
    cy.get('[data-testid="login-button"]').click();

    cy.url().should('include', '/dashboard');
    cy.contains('ν™˜μ˜ν•©λ‹ˆλ‹€').should('be.visible');
  });

  it('잘λͺ»λœ λΉ„λ°€λ²ˆν˜Έλ‘œ μ—λŸ¬λ₯Ό ν‘œμ‹œν•œλ‹€', () => {
    cy.get('[data-testid="email"]').type('user@example.com');
    cy.get('[data-testid="password"]').type('wrong');
    cy.get('[data-testid="login-button"]').click();

    cy.contains('λΉ„λ°€λ²ˆν˜Έκ°€ μΌμΉ˜ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€').should('be.visible');
  });
});

How It's Said in Practice

Senior: "The E2E tests keep failing flakily in this release, and CI reliability is dropping. We need to find the cause."

Junior: "Is it only specific tests? It looks like a timeout issue β€” should we switch from cy.wait to assertion-based waiting?"

Senior: "Sounds good. And let's manage flaky tests separately and add retry logic until they stabilize."

Interviewer: "What do you think about test coverage?"

Candidate: "I think what matters more than the coverage number is whether the important business logic is tested. I aim for 80% coverage, but I cover critical payment and authentication logic close to 100%. Testing even simple getters/setters is inefficient, so I focus on complex conditional branches and edge cases."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

Reviewer: "There's too much mocking in this test. It may differ from actual behavior, so I'd like us to add one integration test."

Developer: "I mocked the DB because connecting to it seemed likely to slow things down, but I'll spin up a test DB container and add an integration test."

Cautions

Learn More