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

本文详细描述了如何使用Karma进行工程初始化,包括配置测试框架(Jasmine)、选择PhantomJS浏览器,以及创建src和test目录并编写测试用例。作者还分享了生成的karma.conf.js配置文件,并提到提供前端开发学习资源和面试题库的学习价值。
摘要由CSDN通过智能技术生成

接下来对我们的工程进行初始化:

karma init

之后会弹出一些选项,其中包含了一些初始化的配置工作,使用上下方向键可以在配置项之间进行切换。我这里选择使用 Jasmine 测试框架,使用 PhantomJS 无界面浏览器,整体的配置选项如下:

myKarmDemo 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

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;

}

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

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

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

img

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

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

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

最后

整理面试题,不是让大家去只刷面试题,而是熟悉目前实际面试中常见的考察方式和知识点,做到心中有数,也可以用来自查及完善知识体系。

《前端基础面试题》,《前端校招面试题精编解析大全》,《前端面试题宝典》,《前端面试题:常用算法》PDF完整版点击这里免费领取

前端面试题宝典

前端校招面试题详解

础面试题》,《前端校招面试题精编解析大全》,《前端面试题宝典》,《前端面试题:常用算法》PDF完整版点击这里免费领取**

[外链图片转存中…(img-knwE7AqK-1713781815236)]

[外链图片转存中…(img-LRXCHrbs-1713781815236)]

[外链图片转存中…(img-zRbERuqP-1713781815237)]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值