Archive

TDD 테스트 주도 개발

mandarinko 2022. 2. 27. 18:18

TDD 테스트 주도 개발

1) TEST

1. UI Testing

  • User Interface Testing
  • 실제로 앱이랑 상호작용하는 테스트
  • 특정 코드에 대한 내부 지식이 없어도 가능합니다
  • 자동화 할 수 있지만, UI Testing은 자동화 하기가 가장 까다롭고, 실행하기도 까다롭습니다.

    2. Integrating Testing

  • 두개 이상의 기능을 종합하여 하는 테스트 입니다.
  • 서버를 띄우고, DB에 접근하고, 기능을 구현하는 다수의 기능이 함께 동작하는 테스트입니다.
  • httpie 또는 postman을 사용해서 Json response가 제대로 출력되는지 테스트하는 방법입니다.

3. Unit Testing

  • 내가 작성한 코드의 가장 작은 단위인 함수를 테스트하는 방법입니다.


2) Unit test 장점

  • 1,2 번은 사람이 직접 테스트 하지만 unit test는 스크립트로 한번에 수행하기 용이합니다.
  • 유지보수에 용이하며, 새로운 기능 추가 할 때 이전에 통과한 test를 재 실행해서 문제 있는 부분만 수정하면 됩니다.
  • 코드변경시, 변경한 부분으로 인한 영향도를 쉽게 파악할 수 있습니다.


3) TDD

Test Driven Development

How

  • test코드를 먼저 작성하고 test를 통과 할 수 있는 source코드 작성합니다.

    Why

  • 유지보수에 용이합니다.
  • 개발시간 << 유지보수 기간


4) Mocha

모카(mocha)는 테스트 코드를 돌려주는 테스트 러너입니다.[공식 사이트]

  • 테스트수트 : 테스트 환경 mocha에서는 describe()로 구현합니다.
  • 테스트케이스 : 실제 테스트를 말하며, mocha에서는 it()으로 구현합니다.

    Example

  • Test code example
    var assert = require('assert');
    

describe('Array', function() {
describe('#indexOf()', function() {
it('should return -1 when the value is not present', function() {
assert.equal([1, 2, 3].indexOf(4), -1);
});
});
});

- Response example
```bash
  Array
    #indexOf()
      ✓ should return -1 when the value is not present


  1 passing (9ms)

왜 Describe, it이 나누어 사용해야 할까요..?

  • describe는 테스트의 틀을 구성하는 과정입니다.
  • it이 실제로 함수를 테스트 하는 과정입니다.
  • Test code example
    unction Foo() {
    }
    

describe("Foo", function () {
var foo;
beforeEach(function () {
foo = new Foo();
});
describe("#clone", function () {
beforeEach(function () {
// Some other hook
});
it("clones the object", function () {
});
});
describe("#equals", function () {
it("returns true when the object passed is the same", function () {
});
it("returns false, when...", function () {
});
});
afterEach(function () {
// Destroy the foo that was created.
// foo.destroy();
});
});

function Bar() {
}

describe("Bar", function () {
describe("#clone", function () {
it("clones the object", function () {
});
});
});

- Response example
```bash
  Foo
    #clone
      ✓ clones the object 
    #equals
      ✓ returns true when the object passed is the same 
      ✓ returns false, when... 

  Bar
    #clone
      ✓ clones the object 


  4 passing (4ms)
  • Test DB구성 또한 describe에서 하고 각 함수별로 it에서 하면 좋겠죠??


5) Should [공식문서]

Should는 node의 Test code에서 Assert대신 사용하는 ThirdParty Lib입니다.

왜 Should를 사용해야 하나요

  • Nodejs 공식문서를 살펴보면 test code 작성에 있어서 assert대신 thirdparty lib를 사용하라고 합니다.
  • Nodejs의 Assert는 Assertionerror을 반환하기 떄문입니다.
  • Should는 결과값이 기대값과 다른 경우 어떻게 다른지 표현이 되지만, Assertionerror은 error에 불과하기 때문에 Should를 사용해야 합니다.


6) Supertest [공식문서]

Supertest는 express integrating testing Lib입니다.

Example

  • Test code example
    const request = require('supertest');
    const express = require('express');
    

const app = express();

app.get('/user', function(req, res) {
res.status(200).json({ name: 'john' });
});

request(app)
.get('/user')
.expect('Content-Type', /json/)
.expect('Content-Length', '15')
.expect(200)
.end(function(err, res) {
if (err) throw err;
});
```