Karma with Travis CI
以下参考 taobaofed.org/blog/2016/0…
what's Karma
官方复制: test runner that fits all our needs.
Karma 的主要目标是为了开发人员提供一个高效的测试环境,不需要设置很多配置的环境,而是可以编写代码并从测试中获取即时的反馈.
goal
- 在真实环境中测试
- 支持远程控制
- 执行速度快
- 可以跟第三方 IDE 进行交互
- 支持 ci 服务
- 高扩展性,支持插件开发
- 支持调试
advance
Karma就是对JsTestDriver的改进,提供了文件监听以及预处理能力,文件监听`很重要,它方便与各种编辑器接入, 减少用户运行单测的成本,直接保存文件就可以了, 下面是一张各种工具的对比图
outline of workline
karma 设计目标主要有下面四点:
- 高效
- 扩展性
- 运行在真实设备
- 无缝的使用流程
karma 是一个典型的 C/S 程序,包含 client 和 server ,通讯方式基于 Http ,通常情况下,客户端和服务端基本都运行在开发者本地机器上。
Karma 是用 JavaScript 实现的, server 端运行在 Node.js 环境下, client 运行在浏览器环境下, 为什么选择使用 Node.js
- 支持多个平台
- 用一种编程语言来解决 server 和 client 端的实现
- 它是一个多产的平台( npm 上有很多不错的模块)
- 想深入探索异步编程
What's CI
CI: Continuous Integration(持续集成)
-
持续集成经常合并小代码更改的实践,而不是在开发周期结束时合并大量的更改,目标是通过以较小的增量开发和测试来构建更健康的软件
-
作为一个持续集成平台,Travis CI通过自动构建和测试代码更改来支持您的开发过程,并提供有关变更成功的即时反馈。 Travis CI还可以通过管理部署和通知来自动化开发过程的其他部分。
总之: 只要代码有变更,就自动运行构建和测试,反馈运行结果。确保符合预期以后,再将新代码"集成"到主干。持续集成的好处在于,每次代码的小幅变更,就能看到运行结果,从而不断累积小的变更,而不是在开发周期结束时,一下子合并一大块代码。
Prerequisites
- github 帐户
- GitHub上托管的项目的所有者权限
Start
-
go to Travis-ci.com and Sign up with GitHub.
-
接受Travis CI的授权
-
允许你的项目库可以使用travis CI
-
Click the green Activate button, and select the repositories you want to use with Travis CI.
-
添加.travis.yml文件到项目 工程中
nodejs 默认设置是用于安装依赖项的bundle install,以及用于构建项目的rake。
language: node_js
node_js:
- "node"
- "10.16.0"
- git commit travis.yml文件
- 通过访问Travis CI并选择存储库,检查构建状态页面以查看构建是否通过构建命令的返回状态通过或失败。
- Check the build status page to see if your build passes or fails according to the return status of the build command by visiting Travis CI and selecting your repository.
demo
安装依赖
- 安装 karma
- npm install -g karma-cli
- npm install
- karma init
- Which g framework do you want to use ? (mocha)
- Do you want to use Require.js ? (no)
- Do you want to capture any browsers automatically ? (Chrome)
- What is the location of your source and test files ? (cdn.bootcss.com/jquery/2.2.…, node_modules/should/should.js, test/**.js)
- Should any of the files included by the previous patterns be excluded ? ()
- Do you want Karma to watch all the files and run the tests on change ? (yes)
编写测试文件
### test
it('should have jQuery', function () {
if (!window.jQuery) {
throw new Error('查看下 karma.conf.js 配置项 files 是否正确')
}
});
本地启动测试
karma start:

编写travis.yml文件,提交到 travis.io
### travis.yml 配置文件
language: node_js
node_js:
- "6"
addons:
chrome: stable
services:
- xvfb
before_install:
npm install karma-cli -g
script:
- npm run start

