单元测试(模块测试)是开发者编写的一小段代码,用于检验被测代码的一个很小的、很明确的功能是否正确。通常而言,一个单元测试是用于判断某个特定条件(或者场景)下某个特定函数的行为。

Karma是一个基于Node.js的JavaScript测试执行过程管理工具( Test Runner ).。该工具可用于测试所有主流Web浏览器, 也可集成到CI ( Continuous integration ) 工具, 也可和其他代码编辑器一起使用.。这个测试工具的一个强大特性就是, 它可以监控(Watch)文件的变化, 然后自行执行。

jasmine 是一个行为驱动开发(TDD)测试框架, 一个js测试框架,它不依赖于浏览器、dom或其他js框架。具体语法参照:官方api

1.1 使用 Karma 对代码进行单元测试,首先需要安装一系列的相关插件。

创建fetest的文件夹,并进入

安装karma

npm install -g karma-cli复制代码
npm install karma --save-dev复制代码

安装各种相关的插件

npm install karma-jasmine karma-phantomjs-launcher jasmine-core --save-dev 复制代码

初始化,整体的配置选项如下:

karma init 复制代码
Which testing framework do you want to use ? Press tab to list possible options. Enter to move to the next question. > jasmine  Do you want to use Require.js ? This will add Require.js plugin. Press tab to list possible options. Enter to move to the next question. > no  Do you want to capture any browsers automatically ? Press tab to list possible options. Enter empty string to move to the next question. > PhantomJS > What is the location of your source and test files ? You can use glob patterns, eg. "js/*.js" or "test/**/*Spec.js". Enter empty string to move to the next question. >  Should any of the files included by the previous patterns be excluded ? You can use glob patterns, eg. "**/*.swp". Enter empty string to move to the next question. >  Do you want Karma to watch all the files and run the tests on change ? Press tab to list possible options. > no复制代码

启动karma

karma start复制代码

出现一下界面代表成功,可进行下一步操作,第一次需要安装phantomjs

npm install -g phantomjs复制代码

初始化完成之后,会在我们的项目中生成一个 karma.conf.js 文件,这个文件就是 Karma 的配置文件。

// Karma configuration // Generated on Sat Jun 02 2018 11:06:15 GMT+0800 (中国标准时间)  module.exports = function(config) {   config.set({      // base path that will be used to resolve all patterns (eg. files, exclude)     basePath: '',       // frameworks to use     // available frameworks: https://npmjs.org/browse/keyword/karma-adapter     frameworks: ['jasmine'], //使用何种断言库       // list of files / patterns to load in the browser     files: [        './unit/**/*.js',     //被测试的代码路径        './unit/**/*.spec.js' //测试代码路径     ],       // list of files / patterns to exclude     exclude: [     ],       // preprocess matching files before serving them to the browser     // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor     preprocessors: {     },       // test results reporter to use     // possible values: 'dots', 'progress'     // available reporters: https://npmjs.org/browse/keyword/karma-reporter     reporters: ['progress'],      // web server port     port: 9876,       // enable / disable colors in the output (reporters and logs)     colors: true,       // level of logging     // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG     logLevel: config.LOG_INFO,       // enable / disable watching file and executing tests whenever any file changes     autoWatch: false,       // start these browsers     // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher     browsers: ['PhantomJS'],       // Continuous Integration mode     // if true, Karma captures browsers, runs the tests and exits     singleRun: true,//执行完是否退出      // Concurrency level     // how many browser should be started simultaneous     concurrency: Infinity   }) }复制代码

创建unit文件夹,并创建index.js、index.spec.js

index.js

function add (num){     if(num == 1){         return 1;     }else{         return num+1;     }  }复制代码

index.spec.js

describe("测试简单基本函数", function() {     it("+1测试", function() {         expect(add(1)).toBe(1);         expect(add(2)).toBe(5);     }); });复制代码

启动karma

karma start复制代码

成功了一个,错误一个。

1.2 使用 karma-coverage测试代码覆盖率

安装karma-coverage

npm install karma-coverage --save-dev复制代码

创建一个docs文件夹,用来存放生成的的测试文件,配置 karma.conf.js 文件:

// Karma configuration // Generated on Sat Jun 02 2018 19:49:27 GMT+0800 (中国标准时间)  module.exports = function(config) {   config.set({      // base path that will be used to resolve all patterns (eg. files, exclude)     basePath: '',       // frameworks to use     // available frameworks: https://npmjs.org/browse/keyword/karma-adapter     frameworks: ['jasmine'],       // list of files / patterns to load in the browser     files: [         './unit/**/*.js',         './unit/**/*.spec.js'     ],       // list of files / patterns to exclude     exclude: [     ],       // preprocess matching files before serving them to the browser     // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor     preprocessors: {         'unit/**/*.js': ['coverage']//对那些文件生成代码覆盖率检查      },       // test results reporter to use     // possible values: 'dots', 'progress'     // available reporters: https://npmjs.org/browse/keyword/karma-reporter      reporters: ['progress','coverage'],//添加'coverage'      coverageReporter: {           type : 'html',           //生成html页面           dir : './docs/coverage/' //存放html页面位置     },      // web server port     port: 9876,       // enable / disable colors in the output (reporters and logs)     colors: true,       // level of logging     // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG     logLevel: config.LOG_INFO,       // enable / disable watching file and executing tests whenever any file changes     autoWatch: false,       // start these browsers     // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher     browsers: ['PhantomJS'],       // Continuous Integration mode     // if true, Karma captures browsers, runs the tests and exits     singleRun: true,      // Concurrency level     // how many browser should be started simultaneous     concurrency: Infinity   }) } 复制代码

启动karma

karma start复制代码

会在docs中生成一个coverage文件夹,里面有生成的测试代码覆盖率的可视化html页面。

打开index.html

修改index.spec.js

describe("测试简单基本函数", function() {     it("+1测试", function() {         expect(add(1)).toBe(1);     }); });复制代码
karma start复制代码

.Karma+Jasmine+karma-coverage的更多相关文章

  1. Karma +Jasmine+ require JS进行单元测试并生成测试报告、代码覆盖率报告

    1. 关于Karma Karma是一个基于Node.js的JavaScript测试执行过程管理工具(Test Runner). 该工具可用于测试所有主流Web浏览器,也可集成到CI(Continuou ...

  2. karma+jasmine自动化测试

    1.安装nodejs,进入项目目录 2.安装karma和相关插件 npm install karma --save-dev npm install karma-jasmine karma-chrome ...

  3. 使用karma+jasmine做单元测试

    目的 使用karma和jasmine来配置自动化的js单元测试. Karma和Jasmine Karma是由Angular团队所开发的一种自动化测试工具.链接:http://karma-runner. ...

  4. 搭建Karma+Jasmine的自动化单元测试

    最近在打算将以前的代码进行重构,过程中发现自己不写自动化测试代码,而是手动的写,这样并不好,所以就学了Karma+Jasmine的自动化单元测试,以后写代码尽量要写自动化单元测试,也要测一下istan ...

  5. 学习Karma+Jasmine+istanbul+webpack自动化单元测试

    学习Karma+Jasmine+istanbul+webpack自动化单元测试 1-1. 什么是karma?  Karma 是一个基于Node.js的Javascript测试执行过程管理工具.该工具可 ...

  6. 前端单元测试环境搭建 Karma Jasmine

    Karma 官网On the AngularJS team, we rely on testing and we always seek better tools to make our life e ...

  7. jasmine+karma 自动化单元测试

    测试的必须性 相信大家都知道测试的必要性,测试先行的概念.不过,写了这么多年的代码,除了之前用java的时候写过一些测试用例,还真的很少写过前端的测试用例,或者做一些自动化测试.感觉做单元测试还是很有 ...

  8. Karma+Jasmine实现自动化单元测试

    1.Karma介绍 Karma是Testacular的新名字,在2012年google开源了Testacular,2013年Testacular改名为Karma.Karma是一个让人感到非常神秘的名字 ...

  9. angular测试-Karma + Jasmine配置

    首先讲一下大致的流程: 需要node环境,首先先要安装node,node不会?请自行搜索.版本>0.8 安装node完成之后先要测试下npm是否测试通过,如下图所示 首先看下目录结构 目录为:F ...

  10. angularJS测试一 Karma Jasmine Mock

    AngularJS测试 一 测试工具 1.NodeJS领域:Jasmine做单元测试,Karma自动化完成单元测试,Grunt启动Karma统一项目管理,Yeoman最后封装成一个项目原型模板,npm ...

随机推荐

  1. DEV 控件使用之:TreeList

    使用DEV控件也有一段时间了,一直想写点东西.最近又使用到TreeList控件,这个控件对于刚使用的人来说确实不好掌握.我想把自己知道的写下来,让还不熟悉的慢慢学会使用,对于会使用的大家交流下.如果有 ...

  2. java实现八大排序算法

    Arrays.sort() 采用了2种排序算法 -- 基本类型数据使用快速排序法,对象数组使用归并排序. java的Collections.sort算法调用的是归并排序,它是稳定排序 方法一:直接插入 ...

  3. k8s网络之Calico网络

    k8s网络主题系列: 一.k8s网络之设计与实现 二.k8s网络之Flannel网络 三.k8s网络之Calico网络 简介 Calico 是一种容器之间互通的网络方案.在虚拟化平台中,比如 Open ...

  4. Asp.net core 3.0

    序言 我的看法:如果你未来五到十年还打算靠 ASP.NET 吃饭,ASP.NET MVC 一定要学,写 WebForm 工作机会将变得很少,具备 MVC 技能才有本钱跟年轻小伙子们抢饭碗,很高比例的 ...

  5. Optimal Marks SPOJ 839

    这题远超其他题非常靠近最小割的实际意义: 割边<=>付出代价<=>决定让两个点的值不相同,边权增加 最小割<=>点的值与s一个阵营的与s相同,与t一个阵营的与t相同 ...

  6. Kafka(二)设计原理

    1.持久性 kafka使用文件存储消息,这就直接决定kafka在性能上严重依赖文件系统的本身特性.且无论任何OS下,对文件系统本身的优化几乎没有可能.因为kafka是对日志进行append操作,因此磁 ...

  7. PC端判断浏览器类型及移动端判断移动设备类型

    浏览器代理检测,可以检测出来用户使用的浏览器类型,也可以检测浏览器所在的操作系统 navigator.userAgent (1).判断浏览器类型 var t = navigator.userAgent ...

  8. 【题解】狼抓兔子—BZOJ1001。

    (胡扯时间)今天炒鸡无聊就打算BZOJ开始从第一道题开始顺着打,这样未来一段时间内也就有事干了.结果发现A+B切掉后就遭遇了一个"小小"的瓶颈(真不友好. 好了说题说题.看题第一眼 ...

  9. CF1153D Serval and Rooted Tree

    题目地址:CF1153D Serval and Rooted Tree 挺好玩儿也挺考思维的一道题 思路:树形DP+贪心 数组 \(d\) 维护这样一个值: 对于一个节点 \(x\) ,它的值最大可以 ...

  10. java 日常学习记录-反射

    Hero类 package helloworld; public class Hero { public String name; //姓名 public float hp; //血量 public ...