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

本文介绍了如何使用Karma进行JavaScript单元测试,包括配置文件的生成和修改,以及如何在项目中执行测试。作者还分享了在测试过程中遇到的问题及解决方法,旨在帮助前端开发者提升技能和学习资源的选择.
摘要由CSDN通过智能技术生成

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年进入阿里一直到现在。

深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

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

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

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

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:前端)

最后

资料过多,篇幅有限,需要文中全部资料可以点击这里获取前端面试资料PDF完整版!

自古成功在尝试。不尝试永远都不会成功。勇敢的尝试是成功的一半。

oPEF.jpg" />

最后

[外链图片转存中…(img-nNUBylL5-1712714692749)]

[外链图片转存中…(img-Phw3Y4Ck-1712714692749)]

资料过多,篇幅有限,需要文中全部资料可以点击这里获取前端面试资料PDF完整版!

自古成功在尝试。不尝试永远都不会成功。勇敢的尝试是成功的一半。

  • 25
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值