Coffee + mocha + Travis-CI 单元测试与覆盖率报告
来源:互联网 发布:微商怎么上淘宝找货源 编辑:程序博客网 时间:2024/06/10 03:14
在调研了几个库之后,我选用了 coffee-coverage 和 mocha 这个两个库。
"devDependencies": { "mocha": "^1.21.5", "chai": "^1.9.2", "coffee-coverage": "^0.4.2"}
在 test/support/env.coffee
引入 coffee-coverage:
if process.env.COV_TEST == 'true' require('coffee-coverage').register path: 'relative' basePath: "#{__dirname}/../.." exclude: ['test', 'node_modules', '.git', 'sample', 'static'] initAll: true
coffee-coverage 的文档上写的 exlude 示例是 exclude: ['/test', '/node_modules', '/.git']
, 但我在被坑了一个半小时之后才发现前面不能加斜杠.....
我发了个 Pull Request, 目前还没被处理。
在 test/app.coffee
里写第一个测试,
describe 'app', -> it 'should can startup', -> # 在我的机器上启动需要差不多 15 秒时间..... @timeout 20000 require '../../app'
mocha 默认的测试报告只会显示测试的通过情况,不会显示测试覆盖率,因此我们要拓展一下 mocha 默认的测试报告,在 test/support/reporter-cov-summary.js
,
var fs = require('fs');var util = require ('util');var Spec = require('mocha/lib/reporters/spec');exports = module.exports = CovSummary;function CovSummary(runner) { Spec.call(this, runner); runner.on('end', report);}function report() { var cov = global._$jscoverage || {}; var files = Object.keys(cov); var covered_lines = 0; var total_lines = 0; files.forEach(function(file) { cov[file].forEach(function(line) { if(line !== undefined) { total_lines ++; if (line !== 0) { covered_lines ++; } } }); }); var covered = (covered_lines / total_lines * 100).toFixed(1); console.log(util.format('Coverage Summary: %s lines of %s lines, %s% covered \n', covered_lines, total_lines, covered));}CovSummary.prototype.__proto__ = Spec.prototype;
这个文件是用 JavaScript 写的,我实在没搞清楚 mocha 怎么直接用 Coffee 的测试报告;它会在测试运行结束后输出一个覆盖率概要,类似于:
Coverage Summary: 305 lines of 2056 lines, 14.8% covered
在 package.json
中加上 scripts:
"scripts": { "start": "./node_modules/.bin/coffee app.coffee", "test": "COV_TEST=true ./node_modules/.bin/mocha --compilers coffee:coffee-script/register --require test/support/env --reporter test/support/reporter-cov-summary.js -- test", "test-only": "./node_modules/.bin/mocha --compilers coffee:coffee-script/register --require test/support/env -- test", "test-cov-html": "COV_TEST=true ./node_modules/.bin/mocha --compilers coffee:coffee-script/register --require test/support/env --reporter html-cov -- test > coverage-reporter.html"},
start
是启动应用,test
是运行测试和覆盖率报告,test-only
只运行测试,test-cov-html
会生成 HTML 格式的测试覆盖率报告。
然后还要改一下 .gitinore
, 加上 /coverage-reporter.html
.
最后是 .travis.yaml
,
language: node_jsnode_js: - "0.10"
差不多就是这样。
0 0
- Coffee + mocha + Travis-CI 单元测试与覆盖率报告
- 单元测试覆盖率与CI
- VS2015+TFS CI集成中的单元测试与代码覆盖率报告
- Travis CI
- Travis CI
- Travis.ci
- mocha、chai、sinon和istanbul实现100%单元测试覆盖率
- 单元测试与代码覆盖率
- 使用OpenCover生成单元测试覆盖率报告
- 单元测试覆盖率生成网页报告----(一)
- 单元测试覆盖率生成网页报告----(三)
- 单元测试覆盖率生成网页报告----(四)
- 单元测试覆盖率生成网页报告----(二)
- 使用 Gradle 与 Travis CI 进行简单持续集成 II
- Travis CI 持续集成
- travis ci 学习
- 开始使用travis ci
- Travis CI使用笔记
- iOS项目目录结构组织的一些总结
- Coding 即将在 segmenfault 发布一系列官方技术博文 !
- Android Activity的启动模式
- Transform变换
- Coding 官方推出网站服务状态页面啦!
- Coffee + mocha + Travis-CI 单元测试与覆盖率报告
- Activity通过广播进行传值
- cookies
- ubutnu14.04默认登陆界面换个自己喜欢的背景图
- OpenGl第三章后续,纹理,绘制图片,文字
- Android USB gadget probe流程
- cplusplus思考与实践
- 控制台app.config 的 <add key="aspnet:MaxHttpCollectionKeys" value="10000" />无效
- iOS边城之绘图编程(Core Graghics)