テストランナー Karma を使い、 QUnit フレームワークを使って書かれたテストを Chrome ブラウザ上で実行します。
npm install karma-qunit --save-dev
npm install karma-chrome-launcher --save-dev
npm install karma-coverage --save-dev
npm install grunt-karma --save-dev
module.exports = function(grunt) {
grunt.initConfig({
karma: {
unit: {
options: {
frameworks: ['qunit'],
browsers: ['Chrome'],
files: ['test/**/*.js', 'src/**/*.js']
}
}
}
});
grunt.loadNpmTasks('grunt-karma');
};
src
フォルダを作成し、その中に hello.js
という名前で以下のようなファイルを作る
function hello(name) {
if(name) {
return "Hello, " + name;
} else {
return "Bye";
}
}
test
フォルダを作成し、その中に helloTest.js
という名前で以下のようなファイルを作る
QUnit.test( "hello test", function( assert ) {
assert.equal(hello("Shoichi"), "Hello, Shoichi");
});
以下のコマンドを実行すると Chrome ブラウザが起動し、 テストが実行される(テスト結果はコンソール上に表示される)
grunt karma
ファイルを変更し保存するたびに、テスト実行が行われる。
module.exports = function(grunt) {
grunt.initConfig({
karma: {
unit: {
options: {
frameworks: ['qunit'],
browsers: ['Chrome'],
files: ['test/**/*.js', 'src/**/*.js'],
reporters: ['progress', 'coverage'],
preprocessors: {
'src/**/*.js': ['coverage']
},
coverageReporter: {
type : 'html',
dir : 'coverage/'
}
}
}
}
});
grunt.loadNpmTasks('grunt-karma');
};
coverage
フォルダに
カバレッジレポートが作成されるテストランナー Karma を使い、 QUnit フレームワークを使って書かれたテストを PhantomJS ブラウザ上で実行します。
このケースでは CI環境 (Jenkins) 上での実行を想定して、 テスト結果を JUnit 形式の XML ファイルで出力してみます。
npm install karma-qunit --save-dev
npm install karma-phantomjs-launcher --save-dev
npm install karma-junit-reporter --save-dev
npm install karma-coverage --save-dev
npm install grunt-karma --save-dev
module.exports = function(grunt) {
grunt.initConfig({
karma: {
unit: {
options: {
frameworks: ['qunit'],
browsers: ['PhantomJS'],
singleRun: true,
reporters: ['progress', 'junit'],
junitReporter: {
outputFile: 'test-results.xml'
},
files: ['test/**/*.js', 'src/**/*.js']
}
}
}
});
grunt.loadNpmTasks('grunt-karma');
};
test-results.xml
にテスト結果が出力される設定src
フォルダを作成し、その中に hello.js
という名前で以下のようなファイルを作る
function hello(name) {
if(name) {
return "Hello, " + name;
} else {
return "Bye";
}
}
test
フォルダを作成し、その中に helloTest.js
という名前で以下のようなファイルを作る
QUnit.test( "hello test", function( assert ) {
assert.equal(hello("Shoichi"), "Hello, Shoichi");
});
以下のコマンドを実行するとテストが実行される
grunt karma
test-results.xml
に出力されるmodule.exports = function(grunt) {
grunt.initConfig({
karma: {
unit: {
options: {
frameworks: ['qunit'], browsers: ['PhantomJS'], singleRun: true,
reporters: ['progress', 'junit', 'coverage'],
files: ['test/**/*.js', 'src/**/*.js'],
junitReporter: {
outputFile: 'test-results.xml'
},
preprocessors: {
'src/**/*.js': ['coverage']
},
coverageReporter: {
type : 'html', dir : 'coverage/'
}
}
}
}
});
grunt.loadNpmTasks('grunt-karma');
};
coverage
フォルダにカバレッジレポートが作成されるテストランナー Karma を使い、 Jasmine フレームワークを使って書かれたテストを Chrome ブラウザ上で実行します。
npm install karma-jasmine --save-dev
npm install karma-chrome-launcher --save-dev
npm install karma-coverage --save-dev
npm install grunt-karma --save-dev
module.exports = function(grunt) {
grunt.initConfig({
karma: {
unit: {
options: {
frameworks: ['jasmine'],
browsers: ['Chrome'],
files: ['test/**/*.js', 'src/**/*.js']
}
}
}
});
grunt.loadNpmTasks('grunt-karma');
};
src
フォルダを作成し、その中に hello.js
という名前で以下のようなファイルを作る
function hello(name) {
if(name) {
return "Hello, " + name;
} else {
return "Bye";
}
}
test
フォルダを作成し、その中に helloTest.js
という名前で以下のようなファイルを作る
describe('hello', function(){
it('returns "Hello, [name]"', function(){
expect(hello('Shoichi')).toBe('Hello, Shoichi')
});
});
以下のコマンドを実行すると Chrome ブラウザが起動し、 テストが実行される(テスト結果はコンソール上に表示される)
grunt karma
ファイルを変更し保存するたびに、テスト実行が行われる。
module.exports = function(grunt) {
grunt.initConfig({
karma: {
unit: {
options: {
frameworks: ['jasmine'],
browsers: ['Chrome'],
files: ['test/**/*.js', 'src/**/*.js'],
reporters: ['progress', 'coverage'],
preprocessors: {
'src/**/*.js': ['coverage']
},
coverageReporter: {
type : 'html',
dir : 'coverage/'
}
}
}
}
});
grunt.loadNpmTasks('grunt-karma');
};
coverage
フォルダに
カバレッジレポートが作成されるテストランナー Karma を使い、 Jasmine フレームワークを使って書かれたテストを PhantomJS ブラウザ上で実行します。
このケースでは CI環境 (Jenkins) 上での実行を想定して、 テスト結果を JUnit 形式の XML ファイルで出力してみます。
npm install karma-jasmine --save-dev
npm install karma-phantomjs-launcher --save-dev
npm install karma-junit-reporter --save-dev
npm install karma-coverage --save-dev
npm install grunt-karma --save-dev
module.exports = function(grunt) {
grunt.initConfig({
karma: {
unit: {
options: {
frameworks: ['jasmine'],
browsers: ['PhantomJS'],
singleRun: true,
reporters: ['progress', 'junit'],
junitReporter: {
outputFile: 'test-results.xml'
},
files: ['test/**/*.js', 'src/**/*.js']
}
}
}
});
grunt.loadNpmTasks('grunt-karma');
};
test-results.xml
にテスト結果が出力される設定src
フォルダを作成し、その中に hello.js
という名前で以下のようなファイルを作る
function hello(name) {
if(name) {
return "Hello, " + name;
} else {
return "Bye";
}
}
test
フォルダを作成し、その中に helloTest.js
という名前で以下のようなファイルを作る
describe('hello', function(){
it('returns "Hello, [name]"', function(){
expect(hello('Shoichi')).toBe('Hello, Shoichi')
});
});
以下のコマンドを実行するとテストが実行される
grunt karma
test-results.xml
に出力されるmodule.exports = function(grunt) {
grunt.initConfig({
karma: {
unit: {
options: {
frameworks: ['jasmine'], browsers: ['PhantomJS'], singleRun: true,
reporters: ['progress', 'junit', 'coverage'],
files: ['test/**/*.js', 'src/**/*.js'],
junitReporter: {
outputFile: 'test-results.xml'
},
preprocessors: {
'src/**/*.js': ['coverage']
},
coverageReporter: {
type : 'html', dir : 'coverage/'
}
}
}
}
});
grunt.loadNpmTasks('grunt-karma');
};
coverage
フォルダにカバレッジレポートが作成されるgrunt-mocha-test を利用すると Mocha フレームワーク を使って書かれたテストを Node.js 上で実行することができます。
npm install grunt-mocha-test --save-dev
module.exports = function(grunt) {
grunt.initConfig({
mochaTest: {
test: {
src: ['test/**/*.js']
}
}
});
grunt.loadNpmTasks('grunt-mocha-test');
};
src
フォルダを作成し、その中に hello.js
という名前で以下のようなファイルを作る
module.exports = function(name) {
if(name) {
return "Hello, " + name;
} else {
return "Bye";
}
};
test
フォルダを作成し、その中に helloTest.js
という名前で以下のようなファイルを作る
var hello = require('../src/hello.js');
var assert = require('assert');
describe('hello', function(){
it('returns "Hello, [name]"', function(){
assert.equal(hello('Shoichi'), 'Hello, Shoichi');
});
});
以下のコマンドを実行するとテストが実行される
grunt mochaTest
npm install grunt-mocha-test --save-dev
npm install grunt-env --save-dev
npm install xunit-file --save-dev
npm install mocha --save-dev
module.exports = function(grunt) {
grunt.initConfig({
env: {
test: {
XUNIT_FILE: 'test-results.xml'
},
},
mochaTest: {
test: {
options: {
reporter: 'xunit-file',
require: 'xunit-file'
},
src: ['test/**/*.js']
}
}
});
grunt.loadNpmTasks('grunt-env');
grunt.loadNpmTasks('grunt-mocha-test');
grunt.registerTask('default', ['env', 'mochaTest']);
};
test-results.xml
にテスト結果が出力される設定src
フォルダを作成し、その中に hello.js
という名前で以下のようなファイルを作る
module.exports = function(name) {
if(name) {
return "Hello, " + name;
} else {
return "Bye";
}
};
test
フォルダを作成し、その中に helloTest.js
という名前で以下のようなファイルを作る
var hello = require('../src/hello.js');
var assert = require('assert');
describe('hello', function(){
it('returns "Hello, [name]"', function(){
assert.equal(hello('Shoichi'), 'Hello, Shoichi');
});
});
以下のコマンドを実行するとテストが実行される
grunt
test-results.xml
に出力されるnpm install grunt-mocha-test --save-dev
npm install blanket --save-dev
module.exports = function(grunt) {
grunt.initConfig({
mochaTest: {
test: {
options: {
reporter: 'spec',
require: 'coverage/blanket'
},
src: ['test/**/*.js']
},
coverage: {
options: {
reporter: 'html-cov',
quiet: true,
captureFile: 'coverage.html'
},
src: ['test/**/*.js']
}
}
});
grunt.loadNpmTasks('grunt-mocha-test');
};
coverage.html
にカバレッジレポートが出力される設定converage
フォルダを作成し、その下に以下のような内容で blanket.js
を作成する
require('blanket')({
pattern: require('path').join(__dirname, '..', 'src')
});
src
フォルダの下にあるファイルをカバレッジ取得対象とする設定src
フォルダを作成し、その中に hello.js
という名前で以下のようなファイルを作る
module.exports = function(name) {
if(name) {
return "Hello, " + name;
} else {
return "Bye";
}
};
test
フォルダを作成し、その中に helloTest.js
という名前で以下のようなファイルを作る
var hello = require('../src/hello.js');
var assert = require('assert');
describe('hello', function(){
it('returns "Hello, [name]"', function(){
assert.equal(hello('Shoichi'), 'Hello, Shoichi');
});
});
以下のコマンドを実行するとテストが実行される
grunt mochaTest
coverage.html
にカバレッジレポートが出力されるgrunt-webdriver を利用すると、 Mocha と WebdriverIO を用いたテストを行うことができます。
grunt-webdriver
をインストールする
npm install grunt-webdriver --save-dev
module.exports = function(grunt) {
grunt.initConfig({
webdriver: {
options: {
desiredCapabilities: {
browserName: 'chrome'
},
reporter: 'xunit',
output: 'test-results.xml'
},
test: {
tests: ['test/**/*.js']
}
}
});
grunt.loadNpmTasks('grunt-webdriver');
};
test
フォルダの下にある *.js
ファイルを対象としてテストを行うtest-results.xml
にテスト結果を出力するtest
フォルダを作成し、その中に test1.js
という名前で以下のようなファイルを作る
var assert = require('assert');
describe('grunt-webdriver test', function () {
it('checks if title contains the search query', function(done) {
browser
.url('https://github.com')
.setValue('form.js-site-search-form input[type="text"]','grunt-webdriver')
.submitForm('form.js-site-search-form')
.getTitle(function(err,title) {
assert(title.indexOf('grunt-webdriver') !== -1);
})
.end(done);
});
});
以下のコマンドを実行するとテストが実行される
grunt webdriver
test-results.xml
にテスト結果が出力される