javascript自动化测试入门(一)

单元测试框架

  • better-assert (TDD断言库)
  • should.js (BDD断言库)
  • except.js(BDD断言库)
  • chai.js (TDD 、 BDD双模断言)
  • jasmine.js (BDD断言库)
  • NodeJs本身集成 require(‘assert’)
  • Intern 一个大而全的单元测试框架
  • QUnit 一个游离在Jquery左右的测试框架
  • Macaca 一套完整的自动化测试解决方案(阿里巴巴)

BDD:Behaviour Driven Development 行为驱动开发
TDD:Test Driven Development 测试驱动开发

常见测试分类

1、单元测试
2、e2e测试
3、视觉回归测试
4、性能测试

需要自动化测试的理由

在此之前,自己没有在任何项目中使用过自动化测试,主要是工作内容迭代快周期短,完全没有时间去写测试用例,开发完成后就直接移交给测试同学进行测试,他们也是人工完成测试。但自动化的好处就在于,只要用例覆盖的全面,就能大大提高代码质量,减少一些容易忽略的bug,做完迭代修改,跑一遍测试用例,轻松做完回归,也能让自己放心不是。而且对于项目以后的维护和交接都提供了保障作用。

单元测试工具karma

这里简单介绍下karma工具的入门使用,首先保证有node环境,然后就是一顿插件要装

npm install karma-cli -g // 安装全局karma命令
// 安装jasmine断言库,无界面浏览器phantomJs,开发环境即可
npm install karma jasmine-core karma-jasmine karma-phantomjs-launcher -D

初始化karma

karma init

会看到下面一片选项

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.conf.js的文件,这就是karma的配置文件,下面需要对配置文件做一个简单的修改

// Karma configuration
// Generated on Tue May 28 2019 00:10:18 GMT+0800 (GMT+08:00)

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 / 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
  })
}

在karma.conf.js同一目录下创建src和test文件夹,这里的路径与配置文件中file属性配置的路径一致就可以,然后在src中创建index.js,在test中创建index.spec.js

index.js代码如下:

function test (flag) {
    if (flag === 1) {
        return 'hello world';
    } else {
        return 'hello kitty';
    }
}

function sum (x, y) {
    return x + y;
}

index.spec.js的代码如下:

describe('index中包含的方法测试', function(){
    it('test方法测试', function(){
        expect(test(1)).toBe('hello world');
        expect(test(2)).toBe('hello kitty');
    });
    it('sum方法测试', function() {
        expect(sum(1, 2)).toBe(3);
    });
})

然后运行karma start或者把命令集成到package.json中

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "unit": "karma start"
  },

就可以运行npm run unit启动测试,可以得到下面的结果

karma start

28 05 2019 00:29:14.510:INFO [karma-server]: Karma v4.1.0 server started at http://0.0.0.0:9876/
28 05 2019 00:29:14.518:INFO [launcher]: Launching browsers PhantomJS with concurrency unlimited
28 05 2019 00:29:14.527:INFO [launcher]: Starting browser PhantomJS
28 05 2019 00:29:20.933:INFO [PhantomJS 2.1.1 (Windows 8.0.0)]: Connected on socket 19EZePoOha6FPj8uAAAA with id 92401631
PhantomJS 2.1.1 (Windows 8.0.0): Executed 2 of 2 SUCCESS (0.005 secs / 0.003 secs)
TOTAL: 2 SUCCESS

可以看见,两个测试用例都验证成功了
这里只是一个简单的入门介绍,想要尝试在自己的项目中使用,可以去github上看看,推荐看看这个(https://github.com/karma-runner/karma)

另关于jasmine断言的语法,大家可以去官网看看,我找了另一个博主的总结,觉得挺全面的,果断引用jasmine语法

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、linux 1,linux常用命令 2,某个时间段日志的查询 3,linux文件的上传和下载 二、功能测试 1,工作中所遇到的错误 2,测试流程: 3,测试计划元素: 4,测试报告元素: 5,测试点: 6,测试方法: 7,bug相关问题 8,adb常用命令 9,软件测试原则 10,测试用例编写的要素 11,测试用例的设计原则 12,软件产品质量特性 13,android四大组件 14,web测试和app测试的区别 15,app的anr的根本原因 16,app的crash的原因 17,h5页面图片未加载出来问题排查 18,区分原生和h5页面 19,为什么不能用jenkins打包 三、性能测试 1,了解jmeter 2,性能指标 3,如何做性能测试 四、接口测试 1,如何设计接口测试用例 2,为什么要做接口测试 3,接口测试的关注点 4,request处理cookie的三种方式 五、自动化测试 1,自动化核心框架 2,自动化测试的好处 3,自动化的前提 4,自动化测试的场景 5,元素定位的8种方式 6,如果一个元素无法定位,一般会考虑哪些原因 7,driver.close()和driver.quit()的区别 8,自动化脚本断言 9,判断页面元素是否存在 10,js在web自动化中的作用展示 11,自动化代码优化 12,selenium对比RF 13,自动化测试脚本三种等待 14,PO模式 六、HTTP协议 1,HTTP协议特点: 2,HTTP传输原理 3,get和post的区别 4,HTTP响应代码 5,osi七层模型 6,三次握手过程 7,session和cookie的区别 8,tcp和udp的区别 9,sockect通信原理 10,post的三种请求方式 七、数据库 1,sql分类 2,数据库事务特性:ACID 3,mysql索引的类型 4,池化思想 5,redis 6,如何提高数据库运行效率 八、java 1,面向对象的三个特征 2,重写和重载 3,比较sping,sping mvc 4,进程和线程的区别 5,java三层架构 6,处理异常 九、python 1,字符串反转的7种方法 2,new 和 _init_ 3,不使用中间变量交换两个变量的值 4,python四大内置高阶函数 5,python带颜色输出 6,python *args,**kargs用法 7,python常用模块 8,python多线程 9,python发送邮件 10,python操作图像 11,python的replace()方法的使用

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值