天天看点

karma介绍、安装、配置、自动化测试

G-god

转载的各位大佬,请补充完整再转,这个只是非常简单的介绍

1.karma介绍 ( karma官网 http://karma-runner.github.io/ )

Karma - Spectacular Test Runner for Javascript(基于node.js的Javascript测试运行环境)

Karma是Testacular的新名字,在2012年google开源了Testacular,2013年Testacular改名为Karma。

Karma的主要目标是为开发人员提供高效的测试环境。 这种环境不需要设置大量的配置,而是开发人员只需编写代码并从测试中立即获得结果。 从而提高你的生产力和创造力。

在AngularJS团队,我们依靠测试,我们总是寻求更好的工具,让我们的生活更轻松。 这就是我们创造的原因。该工具可用于测试所有主流Web浏览器,也可集成到CI工具,也可和其他代码编辑器(例如webstrom)一起使用。这个测试工具的一个强大特性就是,它可以监控(Watch)文件的变化,然后自行执行,通过console.log显示测试结果。

为什么使用Karma?   你想在真实的浏览器中测试代码。

  你想在多个浏览器(桌面,手机,平板电脑等)测试代码。

  你想在开发过程中在本地执行你的测试。

  您想要在持续集成服务器上执行测试。

  你想在每次保存时执行你的测试。

  你爱你的终端(命令行)。

  你不想让你的(测试)生活陷入困境。

  你想使用 Istanbul 自动生成覆盖率报告。

  你想为你的源文件使用RequireJS。

2.karma安装

首先需要在你的电脑上安装node.js (官网: https://nodejs.org/)

安装karma在项目中$ npm install karma --save-dev

如果您在安装软件包时遇到问题,请查看帮助文档 ( https://docs.npmjs.com/getting-started/installing-npm-packages-locally)

安装插件$ npm install karma-jasmine karma-chrome-launcher jasmine-core --save-dev

3.karma配置

初始化测试 $ karma init (按照提示下一步下一步结束后会生产配置文件:karma.conf.js)

1. Which testing framework do you want to use ? (mocha)

2. Do you want to use Require.js ? (no)

3. Do you want to capture any browsers automatically ? (Chrome)

4. What is the location of your source and test files ? (test/**.js)

5. Should any of the files included by the previous patterns be excluded ? ()

6. Do you want Karma to watch all the files and run the tests on change ? (yes)

4.karma自动化测试

编写符合语法的测试js脚本

test.js :

describe('jQuery', function () {

  it('should have jQuery', function () {

    if (!window.jQuery) {

      throw new Error('查看下 karma.conf.js 配置项 files 是否正确')

    }

  });

});

启动测试 $ karma start

查看浏览器控制台输出信息

  • grunt-karma  (grunt集成karma)
  • gulp-karma (gulp集成karma)

继续阅读