NPM経由でインストールします:
npmインストールnodeunit -g
コマンドラインをサポートし、ブラウザが実行されます。さまざまなアサーション。 node.jsのメソッドのモジュール輸出エクスポートモジュールをオブジェクトにエクスポートする場合、モジュールはユニットテストの基礎となります。次のnode.jsコードを参照してください。
var fs = require( 'fs')、global = require( './ global.js'); var utils = {startwith:function(s1、s){if(s == null || s == "" || length == 0 || s.length> this.length)return false; true;}、/ * guid */getGuid:function(){var guid = ""; for(var i = 1; i <= 32; i ++){var n = math.floor(math.random() * 16.0).toString(16); guid+= n;} return guid;} return; var text = fs.readfilesync(global.logfile、 "utf-8")、_ newlog = text? (text + "/r/n" + log):log; fs.writefile(global.logfile、_newlog、function(err){if(err)throw err;});}}; exports.utils = utils;./global.jsはローカルグローバル変数ファイルです。次に、上記のコードをテストするためにnodeunitを使用します。
var utils = new require( './ utils.js'); this.testforutils = {'testgetGuid':function(test){var guid = utils.utils.getGuid(); test.ok(!! guid、 'getGuidはnullではないはずです。 flag = false; utils.utils.writeLog( "テストメッセージ"); flag = true; test.ok(flag、 'writelog'); test.done();}、 'testStartWithWords':function(test){var name = "ad_123"; test.ok(utils.utils.startwith(name be ")、" ad _ ")、" ok "); test.done();}};Test.okは、私たちが通常言う主張でもあります。 NodeUnitユニットテストプログラムの場合、Node-Inspectorを使用してデバッグすることもできます