简体   繁体   English

如何通过持续集成自动化业力单元测试

[英]How to automate karma unit tests with continuous integration

Currently we run our karma/jasmine unit tests a gulp task: gulp test 目前我们运行我们的业力/茉莉花单元测试一个gulp任务: gulp test

We are trying to figure out how to get circleci to run our tests automatically. 我们正试图弄清楚如何让circleci自动运行我们的测试。 I tried adding gulp test under the test: section of the circle.yml file but I get gulp: command not found . 我尝试在gulp test下添加gulp test: circle.yml文件的部分,但我得到了gulp gulp: command not found But I get the same error if I try something basic like pwd . 但是如果我尝试像pwd这样基本的东西,我会得到同样的错误。 So clearly I'm doing something wrong. 显然,我做错了什么。

I think the same results could be achieved by using the scripts property in the package.json, because circleci runs that automatically, but I'm not sure how to do that. 我认为使用package.json中的scripts属性可以实现相同的结果,因为circleci会自动运行,但我不知道该怎么做。

Here's our circle.yml file... 这是我们的circle.yml文件......

dependencies:
  override:
    - echo PHP rules

test:
  override:
    - gulp test #this doesnt work!

deployment:
  development:
    branch: dev
    heroku:
      appname: ourapp

Here's package.json... 这是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"
}

Just in case it's useful, here's the gulp task... 为了防止它有用,这是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;
    })
});

For consistency, I think the best would be if we could get circleci to run the gulp task, but it's okay if we have to run karma ... manually. 为了保持一致性,我认为最好的情况是如果我们可以让circleci运行gulp任务,但是如果我们必须手动运行karma ...就没关系。

Here's what worked for me: 这对我有用:

dependencies:
  pre:
    - npm install -g gulp

test:
  override:
    - gulp test

machine:
  php:
    version: 5.6.2

The npm install -g gulp was the missing piece for me. npm install -g gulp对我来说是个缺失的部分。 Circleci will detect package.json and run npm install automatically, but I needed the pre: to install it globally in order for my gulp task to work. Circleci将检测package.json并自动运行npm install ,但我需要pre: 全局安装它以便我的gulp任务正常工作。

It's also worth mentioning that circleci automatically knows whether the karma/jasmine tests passed or failed by detecting the exit code. 还值得一提的是,circleci通过检测退出代码自动知道业力/茉莉花测试是否通过或失败。 So this just works as-is. 所以这只是按原样运作。

I'm one of the CircleCI devs. 我是CircleCI开发者之一。 What you'll need to do is to specify a NodeJS version to build with in your circle.yml in order to trigger the NodeJS inference rules - https://circleci.com/docs/language-nodejs 您需要做的是指定要在circle.yml中构建的NodeJS版本,以触发NodeJS推理规则 - https://circleci.com/docs/language-nodejs

Once you've done that we'll run npm install , npm test etc. automatically. 一旦你完成了,我们将自动运行npm installnpm test等。 For npm test to do the right thing you'll need to configure your test script correctly. 对于npm test做正确的事情,您需要正确配置测试脚本。 Best to get that running on your local environment first. 最好先在本地环境中运行。

You'll probably also need to add gulp-cli as a dev dependency too, most NodeJS tools these days split the library and cli binary into separate packages. 您可能还需要将gulp-cli添加为dev依赖项,这些天大多数NodeJS工具将库和cli二进制文件拆分为单独的包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM