mocha

提供: Node.js/JavaScript入門
2014年9月7日 (日) 13:12時点におけるDaemon (トーク | 投稿記録)による版

(差分) ←前の版 | 最新版 (差分) | 次の版→ (差分)
移動: 案内検索
スポンサーリンク

mochaとは、JavaScriptの単体テストで利用されるユニットテストフレームワークです。

読み方

mocha
もか

概要

インストール

$ sudo npm install -g mocha

コマンド

コマンドラインオプション

mochaコマンドのコマンドラインオプションは、以下の通りです。

  Usage: _mocha [debug] [options] [files]
 
  Commands:
 
    init <path>            initialize a client-side mocha setup at <path>
 
  Options:
 
    -h, --help                      output usage information
    -V, --version                   output the version number
    -A, --async-only                force all tests to take a callback (async)
    -c, --colors                    force enabling of colors
    -C, --no-colors                 force disabling of colors
    -G, --growl                     enable growl notification support
    -R, --reporter <name>           specify the reporter to use
    -S, --sort                      sort test files
    -b, --bail                      bail after first test failure
    -d, --debug                     enable node's debugger, synonym for node --debug
    -g, --grep <pattern>            only run tests matching <pattern>
    -gc                             --expose-gc
    -i, --invert                    inverts --grep matches
    -r, --require <name>            require the given module
    -s, --slow <ms>                 "slow" test threshold in milliseconds [75]
    -t, --timeout <ms>              set test-case timeout in milliseconds [2000]
    -u, --ui <name>                 specify user-interface (bdd|tdd|exports)
    -w, --watch                     watch files for changes
    --check-leaks                   check for global variable leaks
    --compilers <ext>:<module>,...  use the given module(s) to compile files
    --debug-brk                     enable node's debugger breaking on the first line
    --globals <names>               allow the given comma-delimited global [names]
    --harmony                       enable all harmony features (except typeof)
    --harmony-collections           enable harmony collections (sets, maps, and weak maps)
    --harmony-generators            enable harmony generators
    --harmony-proxies               enable harmony proxies
    --inline-diffs                  display actual/expected differences inline within each string
    --interfaces                    display available interfaces
    --no-deprecation                silence deprecation warnings
    --no-exit                       require a clean shutdown of the event loop: mocha will not call process.exit
    --no-timeouts                   disables timeouts, given implicitly with --debug
    --opts <path>                   specify opts path
    --prof                          log statistical profiling information
    --recursive                     include sub directories
    --reporters                     display available reporters
    --throw-deprecation             throw an exception anytime a deprecated function is used
    --trace                         trace function calls
    --trace-deprecation             show stack traces on deprecations
    --watch-extensions <ext>,...    additional extensions to monitor with --watch

単体テストを実行する

$ mocha test.js

ファイルを監視し、変更されたら自動的に単体テストを実行する

テストが自動的に実行されるため、すぐにおかしいところに気づけます。

$ mocha -w test.js

簡単な利用例

calc.js

/*
 * calc.js
 * Copyright (C) 2014 kaoru <kaoru@bsd>
 */
module.exports.add = function (a,b) {
        return a + b;
};
 
module.exports.square = function (a) {
        return a * a;
};
 
module.exports.cube = function (a) {
        return a * a * a;
};

test_calc.js

/*
 * test_calc.js
 * Copyright (C) 2014 kaoru <kaoru@bsd>
 */
var assert = require('assert');
var myfuncs = require('./calc.js');
 
describe('myfuncs', function() {
        describe('add', function() {
                it ('第1引数と第2引数を足した結果を返す', function() {
                        assert.equal(myfuncs.add(1,2), 3);
                });
        });
        describe('square', function() {
                it ('2乗した結果を返す', function() {
                        assert.equal(myfuncs.square(2), 4);
                });
        });
        describe('cube', function() {
                it ('3乗した結果を返す', function() {
                        assert.equal(myfuncs.cube(2), 8);
                });
        });
});

実行例

正常にテストが終了した場合の例です。

$ mocha test_calc.js
 
 
  myfuncs
    add
      ✓ 第1引数と第2引数を足した結果を返す
    square
      ✓ 2乗した結果を返す
    cube
      ✓ 3乗した結果を返す
 
 
  3 passing (34ms)

関連項目




スポンサーリンク