运行摩卡咕噜声 – 当testing失败时放弃
我正试图学习这个自动unit testingmalarkey。 我想使用Grunt自动运行我的Mochatesting,并将testing结果输出到一个文件。 据我所知,我需要使用grunt-mocha-cov 。 我有这样的工作:当testing通过Grunt写出结果文件确定。 但是,当其中一个失败,我得到这个:
Running "mochacov:all" (mochacov) task Warning: Use --force to continue. Aborted due to warnings.
并没有创build文件。 谁能告诉我哪里出错了?
我的项目是这样组织的:
我的testing文件夹包含一个文件,test.js,看起来像这样:
var chai = require("chai"), assert = chai.assert, expect = chai.expect; var foobar = { sayHello: function() { return 'Hello World!'; } } describe('Foobar', function() { describe('#sayHello()', function() { it('should work with assert', function() { assert.equal(foobar.sayHello(), 'Hello World!'); }); it('should work with expect', function() { expect(foobar.sayHello()).to.equal('Hello Worlxd!'); }); }); });
package.json有这个:
{ "name": "GruntTest", "version": "0.0.1", "private": true, "devDependencies": { "grunt": "~0.4.1", "grunt-mocha-cli": "~1.3.0", "grunt-contrib-qunit": "~0.3.0", "grunt-contrib-jshint": "~0.6.4", "grunt-mocha": "~0.4.1", "should": "~2.0.1", "chai": "~1.8.1", "grunt-mocha-cov": "0.0.7" }, "description": "Grunt Test", "main": "grunt.js", "dependencies": { "grunt": "~0.4.1" }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "BSD" }
这是我的Gruntfile.js:
module.exports = function(grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), mochacov: { options: { reporter: 'XUnit', require: ['should'], output: 'test-results.xml', bail: false }, all: ['test/*.js'] } }); grunt.loadNpmTasks('grunt-mocha-cov'); grunt.registerTask('default', ['mochacov']); };
编辑
按照xavier的build议,我和mochacov以及xunit-file记者一起工作。 这是我的新改进的Gruntfile,以防其他人使用:
module.exports = function(grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), mochacov: { options: { reporter: 'xunit-file', require: ['should'], bail: false }, all: ['test/*.js'] } }); grunt.loadNpmTasks('grunt-mocha-cov'); grunt.registerTask('default', ['mochacov']); };
terminal会发出警告“由于警告而中止”,但mochacov会创build一个带有testing结果的文件xunit.xml。
尝试一个: https : //github.com/yaymukund/grunt-simple-mocha
与以下记者之一: http : //visionmedia.github.io/mocha/#reporters
或者像这样的: https : //github.com/peerigon/xunit-file
但事实是,你应该从服务器端咕噜一声,并使用Makefile!
这是我使用的一个经典之作:
MOCHA="node_modules/.bin/mocha" _MOCHA="node_modules/.bin/_mocha" JSHINT="node_modules/.bin/jshint" ISTANBUL="node_modules/.bin/istanbul" TESTS=$(shell find test/ -name "*.test.js") clean: rm -rf reports test: $(MOCHA) -R spec $(TESTS) jshint: $(JSHINT) src test coverage: @# check if reports folder exists, if not create it @test -d reports || mkdir reports $(ISTANBUL) cover --dir ./reports $(_MOCHA) -- -R spec $(TESTS) .PHONY: clean test jshint coverage
就是这样,只要安装摩卡,jshint和伊斯坦布尔作为开发依赖,你很好去
有些人会告诉你全球安装这些工具,但这取决于你