🔧 DevOps

Testing

Testing

Verifying that code behaves as expected. Divided into unit, integration, and E2E tests. TDD is test-first development.

Detailed Explanation

Testing is the activity of verifying that software behaves as expected. It detects bugs early, provides a safety net during refactoring, and documents the behavior of code. It is at the core of quality assurance (QA).

In the testing pyramid, tests are layered into unit tests, integration tests, and E2E (end-to-end) tests. Unit tests are the most numerous and fastest, while E2E tests become fewer and slower. A 70/20/10 ratio is recommended.

TDD (Test-Driven Development) writes tests first and then implements the code. It proceeds in a Red (fail) -> Green (pass) -> Refactor (improve) cycle. BDD (Behavior-Driven Development) describes tests in Given-When-Then form from the perspective of user behavior.

In practice, all tests are run automatically in CI. Aim for coverage of 80% or more, but whether critical logic is tested matters more than the number. Use mocking to speed up tests, and fix flaky tests immediately.

Code Example

// Jest 유닛 테스트 예제
describe('calculatePrice', () => {
  // 기본 케이스
  it('should calculate price with no discount', () => {
    const result = calculatePrice(100, 0);
    expect(result).toBe(100);
  });

  // 할인 적용
  it('should apply percentage discount correctly', () => {
    const result = calculatePrice(100, 20);
    expect(result).toBe(80);
  });

  // 엣지 케이스
  it('should handle 100% discount', () => {
    const result = calculatePrice(100, 100);
    expect(result).toBe(0);
  });

  // 에러 케이스
  it('should throw error for negative price', () => {
    expect(() => calculatePrice(-100, 10)).toThrow('Invalid price');
  });
});

// 비동기 테스트
describe('UserService', () => {
  it('should fetch user by id', async () => {
    const mockUser = { id: 1, name: 'John' };
    jest.spyOn(api, 'getUser').mockResolvedValue(mockUser);

    const result = await userService.getUserById(1);

    expect(result).toEqual(mockUser);
    expect(api.getUser).toHaveBeenCalledWith(1);
  });
});

// Integration Test (Supertest)
describe('POST /api/orders', () => {
  it('should create order and return 201', async () => {
    const orderData = { productId: 1, quantity: 2 };

    const response = await request(app)
      .post('/api/orders')
      .send(orderData)
      .set('Authorization', `Bearer ${testToken}`);

    expect(response.status).toBe(201);
    expect(response.body.orderId).toBeDefined();
  });
});

// package.json scripts
// "scripts": {
//   "test": "jest",
//   "test:watch": "jest --watch",
//   "test:coverage": "jest --coverage",
//   "test:ci": "jest --ci --coverage --reporters=default --reporters=jest-junit"
// }

How It's Said in Practice

Senior: "I changed the payment logic. How is the test coverage?"

Junior: "It's 95% for that module, and I've added tests for boundary value cases and error handling."

Interviewer: "Please explain the characteristics of each layer in the testing pyramid."

Candidate: "Unit tests test fast, isolated function/class units. Integration tests verify interactions between modules, and E2E tests verify real user scenarios. The more numerous and faster the lower layers are, the easier debugging is."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

Reviewer: "This test actually calls an external API. Wouldn't it be unstable in CI?"

Developer: "I'll handle it with mocking using mockResolvedValue. I'll separate the real API tests into a separate integration suite."

Cautions

Learn More