블로그 이미지
윤영식
Full Stacker, Application Architecter, KnowHow Dispenser and Bike Rider

Publication

Category

Recent Post

2013. 2. 16. 14:56 Testing, TDD/Test First

JavaScript 의 Unit Test를 위하여 여러 프레임워크가 나왔고, 이중 자유도가 높은 Mocha를 사용하려 한다. Assert에 대해 변경을 하면 BDD, TDD를 할 수 있고, JavaScript Funcational 프로그래밍 테스트에 잘 어울리는 BDD를 기본으로 사용한다



1. Test Framework의 종류 

  - Mocha의 장점이 가장 많다 (5page)



2. Mocha 

  - mocha 명령을 수행하는 위치 하위로 test디렉토리에 있는 스크립트를 자동 수행한다 

  - Assertion의 종류를 선택할 수 있다

    + should.js : describe, it 의 BDD 스타일

    + chai : expect(), assert() 스타일

    + expect.js : expect() 스타일 

  - should.js 는 Object prototype에 assert 모듈을 확장하였다 

  - test/mocha.opts 옵션 파일을 놓으면 자동 테스트시에 해당 옵션을 자동 적용한다

--require should
--reporter dot
--ui bdd
--globals okGlobalA,okGlobalB
--globals okGlobalC
--globals callback*

  --timeout 200


  - Mocha의 BDD 스타일 기본형식 

describe('BDD style', function() {

  before(function() {

    // excuted before test suite

  });

 

  after(function() {

    // excuted after test suite

  });

 

  beforeEach(function() {

    // excuted before every test

  });

 

  afterEach(function() {

    // excuted after every test

  });

   

  describe('#example', function() {

    it('this is a test.', function() {

      // write test logic

    });

  });

});




3. 사용법

  - test 폴더 밑에 mocha.opts 파일 작성

    + coffee-script 지원

    + requrie('should') 할 필요 없이 should.js 모듈 첨부

--compilers coffee:coffee-script

--require should


  - 간단한 테스트 프로그램 작성 

    + should.js API 익히기  : Object prototype을 확장하였으므로 Object.should 사용한다 (직관적인 표현이 좋군)

describe('Array', function() {

describe('#indexOf()', function() {

it('should return -1', function() {

[1,2,3].indexOf(5).should.equal(-1);

})

})

})


  -test 폴더와 같은 dept 위치에서 mocha 수행

[nulpulum:~/development/mongojs] mocha


  ․


  1 test complete (1 ms)



<참조>

  - Mocha 사용법 - Outsider

  - mocha.opts

  - 공식홈페이지 http://visionmedia.github.com/mocha/

  - Test Framework Pros and Cons 비교

posted by 윤영식