常用的前端自动化测试工具介绍 —— Karma

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

Config file generated at “/home/charley/Desktop/myKarmDemo/karma.conf.js”.

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

配置文件比较简单,能够比较轻松的看懂,这里我对原始的配置文件进行简单的修改,结果如下:

// Karma configuration

// Generated on Sun Oct 29 2017 21:45:27 GMT+0800 (CST)

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: [

“./src/**/*.js”,

“./test/**/*.spec.js”,

],

// list of files 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

})

}

然后创建一个 src 目录和一个 test 目录,在其中分别创建 index.js 和 index.spec.js 文件。

我要做的测试内容比较简单,对 index.js 中的两个函数(一个加法函数,一个乘法函数)进行测试。

index.js 文件如下:

// 加法函数

function add(x){

return function(y){

return x + y;

}

}

// 乘法函数

function multi(x){

return function(y){

return x * y + 1;

}

}

index.spec.js 文件如下:

describe(“运算功能单元测试”,function(){

it(“加法函数测试”,function(){

var add5 = add(5)

expect(add5(5)).toBe(10)

});

it(“乘法函数测试”,function(){

var multi5 = multi(5)

expect(multi5(5)).toBe(25)

})

})

单测的代码写好后,就可以使用 karma start 来运行单元测试。由于我们的乘法代码中有错误,因此测试结果是这样的:

myKarmDemo karma start 29 10 2017 22:21:56.283:INFO [karma]: Karma v1.7.1 server started at http://0.0.0.0:9876/ 29 10 2017 22:21:56.287:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency 29 10 2017 22:21:56.294:INFO [launcher]: Starting browser PhantomJS 29 10 2017 22:21:56.549:INFO [PhantomJS 2.1.1 (Linux 0.0.0)]: Connected on socket 0h6boaepSUMwG7l2AAAA with id 44948955 PhantomJS 2.1.1 (Linux 0.0.0) 运算功能单元测试 乘法函数测试 FAILED Expected 26 to be 25. test/index.spec.js:9:31 PhantomJS 2.1.1 (Linux 0.0.0): Executed 2 of 2 (1 FAILED) (0.048 secs / 0.002 secs)

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
droid开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!**

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值