如何通过持续集成自动化业力单元测试
How to automate karma unit tests with continuous integration
目前我们 运行 我们的 karma/jasmine 单元测试 gulp 任务:gulp test
我们正在尝试弄清楚如何让我们的测试自动从 circleci 到 运行。我尝试在 circle.yml 文件的 test:
部分下添加 gulp test
,但我得到 gulp: command not found
。但是如果我尝试像 pwd
这样的基本操作,我会得到同样的错误。很明显我做错了什么。
我认为通过在 package.json 中使用 scripts
属性 可以达到相同的结果,因为 circleci 运行s 是自动的,但我不是确定该怎么做。
这是我们的 circle.yml 文件...
dependencies:
override:
- echo PHP rules
test:
override:
- gulp test #this doesnt work!
deployment:
development:
branch: dev
heroku:
appname: ourapp
这里是package.json...
{
"name": "ourapp",
"private": true,
"description": "An app",
"main": "index.js",
"dependencies": {
"gulp": "~3.8.5"
},
"devDependencies": {
"karma": "~0.12.31",
"karma-chrome-launcher": "~0.1.7",
"jasmine-core": "~2.2.0",
"karma-jasmine": "~0.3.5",
"karma-firefox-launcher": "~0.1.4",
"karma-ie-launcher": "~0.1.5",
"karma-opera-launcher": "~0.1.0",
"karma-phantomjs-launcher": "~0.1.4",
"gulp": "~3.8.10",
"gulp-karma": "0.0.4",
"karma-coverage": "~0.2.7"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "The A Team"
}
为了以防万一,这里是 gulp 任务...
gulp.task('test', function(){
return gulp.src(testFiles)
.pipe(karma({
configFile: 'karma.conf.js',
action: 'run' //change to watch if you want to auto-run
}))
.on('error', function(err){
throw err;
})
});
为了保持一致性,我认为最好的办法是让 circleci 完成 运行 gulp 任务,但如果我们必须 运行 karma ...
也没关系手动。
我是 CircleCI 的开发者之一。您需要做的是在 circle.yml 中指定要构建的 NodeJS 版本,以触发 NodeJS 推理规则 - https://circleci.com/docs/language-nodejs
完成后,我们将自动 运行 npm install
、npm test
等。
为了 npm test
做正确的事情,您需要正确配置您的测试脚本。最好先在您的本地环境中获得 运行ning。
您可能还需要添加 gulp-cli
作为开发依赖项,如今大多数 NodeJS 工具将库和 cli 二进制文件拆分为单独的包。
以下是对我有用的方法:
dependencies:
pre:
- npm install -g gulp
test:
override:
- gulp test
machine:
php:
version: 5.6.2
npm install -g gulp
是我所缺少的部分。 Circleci 会自动检测 package.json 和 运行 npm install
,但我需要 pre:
来 全局安装它 为了我的 gulp 工作任务。
还值得一提的是,circleci 通过检测退出代码自动知道 karma/jasmine 测试是通过还是失败。所以这只是按原样工作。
目前我们 运行 我们的 karma/jasmine 单元测试 gulp 任务:gulp test
我们正在尝试弄清楚如何让我们的测试自动从 circleci 到 运行。我尝试在 circle.yml 文件的 test:
部分下添加 gulp test
,但我得到 gulp: command not found
。但是如果我尝试像 pwd
这样的基本操作,我会得到同样的错误。很明显我做错了什么。
我认为通过在 package.json 中使用 scripts
属性 可以达到相同的结果,因为 circleci 运行s 是自动的,但我不是确定该怎么做。
这是我们的 circle.yml 文件...
dependencies:
override:
- echo PHP rules
test:
override:
- gulp test #this doesnt work!
deployment:
development:
branch: dev
heroku:
appname: ourapp
这里是package.json...
{
"name": "ourapp",
"private": true,
"description": "An app",
"main": "index.js",
"dependencies": {
"gulp": "~3.8.5"
},
"devDependencies": {
"karma": "~0.12.31",
"karma-chrome-launcher": "~0.1.7",
"jasmine-core": "~2.2.0",
"karma-jasmine": "~0.3.5",
"karma-firefox-launcher": "~0.1.4",
"karma-ie-launcher": "~0.1.5",
"karma-opera-launcher": "~0.1.0",
"karma-phantomjs-launcher": "~0.1.4",
"gulp": "~3.8.10",
"gulp-karma": "0.0.4",
"karma-coverage": "~0.2.7"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "The A Team"
}
为了以防万一,这里是 gulp 任务...
gulp.task('test', function(){
return gulp.src(testFiles)
.pipe(karma({
configFile: 'karma.conf.js',
action: 'run' //change to watch if you want to auto-run
}))
.on('error', function(err){
throw err;
})
});
为了保持一致性,我认为最好的办法是让 circleci 完成 运行 gulp 任务,但如果我们必须 运行 karma ...
也没关系手动。
我是 CircleCI 的开发者之一。您需要做的是在 circle.yml 中指定要构建的 NodeJS 版本,以触发 NodeJS 推理规则 - https://circleci.com/docs/language-nodejs
完成后,我们将自动 运行 npm install
、npm test
等。
为了 npm test
做正确的事情,您需要正确配置您的测试脚本。最好先在您的本地环境中获得 运行ning。
您可能还需要添加 gulp-cli
作为开发依赖项,如今大多数 NodeJS 工具将库和 cli 二进制文件拆分为单独的包。
以下是对我有用的方法:
dependencies:
pre:
- npm install -g gulp
test:
override:
- gulp test
machine:
php:
version: 5.6.2
npm install -g gulp
是我所缺少的部分。 Circleci 会自动检测 package.json 和 运行 npm install
,但我需要 pre:
来 全局安装它 为了我的 gulp 工作任务。
还值得一提的是,circleci 通过检测退出代码自动知道 karma/jasmine 测试是通过还是失败。所以这只是按原样工作。