我使用Jasmine和Karma来编写单元测试和代码覆盖。我使用Gulp创建了任务,并在VS 2015更新3中通过任务运行器资源管理器运行它们。
var gulp = require("gulp");
var Server = require('karma').Server;
var remapIstanbul = require('remap-istanbul/lib/gulpRemapIstanbul');
gulp.task('unit-tests', function (done) {
new Server({
configFile: __dirname + '/karma.conf.js'
}, done).start();
});
gulp.task('code-coverage', function () {
return gulp.src('_reports/coverage-javascript.json')
.pipe(remapIstanbul({
reports: {
'json': '_reports/coverage-typescript.json',
'html': '_reports/html-report'
}
}));
});
我想读取生成的html结果文件,即从_reports/html-report/index.html文件在封闭构建或夜间构建。我想使用这个代码覆盖率来执行某些操作,比如当代码覆盖率低于80%或测试失败时停止构建。
我该怎么做呢?
我已经实现了一个解决方案,每当任何单元测试失败时MSBuild都会失败。基本上我在我的项目中写了一个自定义目标。'CompileTypeScript' target后运行的csproj文件。
<PropertyGroup>
<CompileDependsOn>
$(CompileDependsOn);
GulpBuild;
</CompileDependsOn>
</PropertyGroup>
<Target Name="GulpBuild" DependsOnTargets="CompileTypeScript">
<Exec Command="./node_modules/.bin/gulp task-name" />
</Target>
此任务将在Visual studio将TS编译为JS后运行。在构建服务器'Path'变量没有为'gulp'设置,这就是为什么通过node_modules .bin文件夹传递命令。