是什么?

  • [Node assert] (http://nodejs.cn/api/assert.html)

    官方说明:assert 模块提供了断言测试的函数,用于测试不变式。
    有严格模式(strict mode)与遗留模式(legacy mode)两种模式,建议只使用严格模式。
    查看 MDN的等式比较指南,了解更多关于等式比较的信息。

  • should.js

    官方说明:should is an expressive, readable, framework-agnostic assertion library. The main goals of this library are to be expressive and to be helpful. It keeps your test code clean, and your error messages helpful.

    有道翻译如下:
    should是一个富有表现力,可读,框架无关的断言库。这个库的主要目标是表达和帮助。它可以保持你的测试代码整洁,并且错误消息也很有用。

  • mocha

    mocha中文学习文档

    Mocha是一个在Node上运行的特性丰富的JavaScript测试框架。让异步测试变得简单和有趣。Mocha测试是连续运行的,允许灵活和准确的报告,同时将未捕获的异常映射到正确的测试用例

    mocha是JavaScript的一种单元测试框架,单元测试是用来对一个模块、一个函数或者一个类来进行正确性检验的测试工作。以测试为驱动的开发模式最大的好处就是确保一个程序模块的行为符合我们设计的测试用例。在将来修改的时候,可以极大程度地保证该模块行为仍然是正确的。

    小本本:测试框架选型

  • TDD 和 BDD

    TDD:Test-Driven Development(测试驱动开发)
    是敏捷开发中的一项核心实践和技术,在开发功能代码之前,先编写单元测试用例代码,测试代码确定需要编写什么产品代码(。侧重点偏向开发 -> 开发人员)

    BDD:Behavior Driven Development(行为驱动开发)
    是一种敏捷软件开发的技术,它鼓励软件项目中的开发者、QA和非技术人员或商业参与者之间的协作。(侧重点偏向设计 -> 产品、开发者、测试人员)

  • Karma

    The main goal for Karma is to bring a productive testing environment to developers. The environment being one where they don't have to set up loads of configurations, but rather a place where developers can just write the code and get instant feedback from their tests. Because getting quick feedback is what makes you productive and creative.

    有道翻译如下:
    Karma的主要目标是为开发人员提供高效的测试环境。环境是他们不必设置大量配置的环境,而是开发人员只需编写代码并从测试中获得即时反馈的地方。因为获得快速反馈是让您富有成效和创造力的原因。

    小本本: karma Coverage

  • Travis CI

    Travis CI 是目前新兴的开源持续集成构建项目,它与jenkins,GO的很明显的特别在于采用yaml格式,简洁清新独树一帜。

    什么是持续集成?
    注:此处引用文章https://baijiahao.baidu.com/s?id=1587248159486720910&wfr=spider&for=pc

    Travis CI 提供的是持续集成服务(Continuous Integration,简称 CI)。它绑定 Github 上面的项目,只要有新的代码,就会自动抓取。然后,提供一个运行环境,执行测试,完成构建,还能部署到服务器。
    持续集成指的是只要代码有变更,就自动运行构建和测试,反馈运行结果。确保符合预期以后,再将新代码"集成"到主干。

    持续集成的好处在于,每次代码的小幅变更,就能看到运行结果,从而不断累积小的变更,而不是在开发周期结束时,一下子合并一大块代码。

使用Karma

这里介绍使用karma和mocha进行单元测试。断言库使用should。

安装步骤

  • 全局安装 mocha和karma-cli
npm install mocha -g
npm install -g karma-cli
  • 初始化npm包
npm init -y  // -y是默认配置
  • 在项目内安装 karma, mocha, should
npm i karma mocha should --save-dev
  • 初始化测试
karma init
1. Which testing framework do you want to use ? (mocha)  // 默认是jasmine, 按键盘的->键切换成mocha.
2. Do you want to use Require.js ? (no)
3. Do you want to capture any browsers automatically ? (Chrome)
4. What is the location of your source and test files ? (https://cdn.bootcss.com/jquery/2.2.4/jquery.js, node_modules/should/should.js, test/**.js)
5. Should any of the files included by the previous patterns be excluded ? ()
6. Do you want Karma to watch all the files and run the tests on change ? (yes)
  • 启动测试
karma start

也可以在package.json中配置scripts:
运行npm run test

"scripts": {
"test": "karma start"
},

测试

karma 的配置文件中,加入我们的测试文件

// list of files / patterns to load in the browser
files: ['https://cdn.bootcss.com/jquery/2.2.4/jquery.js','node_modules/should/should.js','test/**.js'
],

在test/test.js中写入我们的测试用例:

describe('jQuery', function () {it('should have jQuery', function () {if (!window.jQuery) {throw new Error('查看下 karma.conf.js 配置项 files 是否正确')}})it('should able to get a body', function () {var $body = $('body')$body.length.should.equal(1)$body[0].should.equal(document.getElementsByTagName('body')[0])})describe('should able to trigger an event', function () {var elebefore(function () {ele = document.createElement('button')document.body.appendChild(ele)})it('should able trigger an event', function (done) {$(ele).on('click', function () {done()}).trigger('click')})after(function () {document.body.removeChild(ele)ele = null})})it('should able to request https://raw.githubusercontent.com/FE-star/exercise1/master/test/test.js', function (done) {// 使用 jQuery.ajax 请求 https://raw.githubusercontent.com/FE-star/exercise1/master/test/test.js,并验证是否拿到文件var url = "https://raw.githubusercontent.com/FE-star/exercise1/master/test/test.js"$.ajax({type: "GET",url: url,success: function(msg){done()}});})
})

测试成功:

这里贴一下 karma.conf.js 的基本配置:

// Karma configuration
// Generated on Sat Nov 10 2018 18:53:08 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-adapterframeworks: ['mocha'],// list of files / patterns to load in the browserfiles: ['https://cdn.bootcss.com/jquery/2.2.4/jquery.js','node_modules/should/should.js','test/**.js'],// list of files / patterns to excludeexclude: [],// preprocess matching files before serving them to the browser// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessorpreprocessors: {},// test results reporter to use// possible values: 'dots', 'progress'// available reporters: https://npmjs.org/browse/keyword/karma-reporterreporters: ['progress'],// web server portport: 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_DEBUGlogLevel: config.LOG_INFO,// enable / disable watching file and executing tests whenever any file changesautoWatch: true,// start these browsers// available browser launchers: https://npmjs.org/browse/keyword/karma-launcherbrowsers: ['Chrome'],// Continuous Integration mode// if true, Karma captures browsers, runs the tests and exitssingleRun: false,// Concurrency level// how many browser should be started simultaneousconcurrency: Infinity})
}

接入Travis CI

说明: 这里演示把托管在Github上的项目使用Travis Ci来做集成测试。

准备工作:

  • 打开https://travis-ci.org/ travis-cli官网。
  • 把Github的项目同步到Travis Ci的管理后台:

  • 针对某个项目打开开关,如exercise3打开了开关:

接下来在文件的根目录下创建一个文件夹.travis.yml
简单配置:

# 使用的语言
language: node_js
# 使用的nodejs版本
node_js:- "8"
addons:chrome: stable
before_script:- "export DISPLAY=:99.0"- "sh -e /etc/init.d/xvfb start"
# 上面句话是为了在Linux系统下默认没有Chrome, 无法启动而测试不通过。install:- npm install -g karma-cli

把修改好的代码推送到github仓库上,开开travis官网,你就发现你的项目开始构建了。

转载于:https://www.cnblogs.com/arissy/p/9940341.html

介绍Node assert, should.js, mocha, Karma, Travis CI相关推荐

  1. 利用Travis CI 让你的github项目持续构建(Node.js为例)

    Travis CI 是目前新兴的开源持续集成构建项目,它与jenkins,GO的很明显的特别在于采用yaml格式,简洁清新独树一帜.目前大多数的github项目都已经移入到Travis CI的构建队列 ...

  2. e2e 自动化集成测试 架构 实例 WebStorm Node.js Mocha WebDriverIO Selenium Step by step (四) Q 反回调...

    上一篇文章"e2e 自动化集成测试 架构 京东 商品搜索 实例 WebStorm Node.js Mocha WebDriverIO Selenium Step by step (三) Sq ...

  3. mocha 测试 mysql_e2e 自动化集成测试 架构 实例 WebStorm Node.js Mocha WebDrive

    e2e 自动化集成测试 架构 京东 商品搜索 实例 WebStorm Node.js Mocha WebDriverIO Selenium Step by step 二 图片验证码的识别 , 下面讲一 ...

  4. 测试篇二:关于测试(mocha+karma)

    断言 node assert assert模块提供了一组简单的断言测试,可用于测试不变量. API文档 断言库 should.js API文档 测试框架(mocha) mocha(摩卡)是运行测试的工 ...

  5. node MySQL buffer_node.js中buffer方法使用说明

    熟悉JavaScript的童鞋应该对Node.js都不陌生,没错Node.js是一个基于Chrome JavaScript运行时建立的平台,用于方便地搭建响应速度快.易于扩展的网络应用.Node.js ...

  6. Travis CI mysql_DevOps工具介绍连载(34)——Travis CI

    原标题:DevOps工具介绍连载(34)--Travis CI Travis CI 持续集成 Travis CI 是在线托管的持续集成服务,绑定Github项目,抓取新的代码自动进行测试构建,甚至自动 ...

  7. npm ERR! gifsicle@5.2.0 postinstall: `node lib/install.js`

    npm ERR! gifsicle@5.2.0 postinstall: node lib/install.js 解决方案:npm install --ignore-scripts 使用–ignore ...

  8. npm install 时候报错 gifsicle@5.2.0 postinstall: `node lib/install.js`

    npm install 时候报错 gifsicle@5.2.0 postinstall: `node lib/install.js` > gifsicle@5.2.0 postinstall / ...

  9. vue端口号被占用报错npm ERR! @1.0.0 dev: `node build/dev-server.js`

    本群面相web开发爱好者以及同行,共同探讨研究技术,分享交流经验,帮助新人成长,大牛技术精进,js发展日新月异闭门造车是没有出路的,有问必答,共同进步.求职招聘qq群 626448857 附前后端技术 ...

最新文章

  1. 【青少年编程】【三级】小鸡吃虫
  2. mint-ui的Loadmore组件使用示例
  3. lnmp基于fastcgi实现nginx_php_mysql的分离_LNMP基于FastCGI实现Nginx,PHP,MySQL的架构分离...
  4. C#LeetCode刷题之#475-供暖器(Heaters)
  5. spring学习笔记四(注入Bean属性)
  6. Python监控屏幕并截图保存
  7. 从零实现深度学习框架——实现常见运算的计算图(上)
  8. java下面哪些定义正确_Java认证考试题
  9. 数学建模之相关性分析1
  10. 转换世界地图到球纹理图
  11. 全球与中国航天工业注塑机市场深度研究分析报告
  12. Shader特效之图片波动炫光效果
  13. 利用Flourish制作动态条形图
  14. MFC编辑框数据读写
  15. 做了个后末日朋克风的梦
  16. 处理工具提示的TTN_NEEDTEXT通知
  17. java sqlserver 插入数据_java中怎样向SQLserver中插入数据
  18. 橄榄油可以用来炒菜吗
  19. 词汇量计算机,都在说词汇量,究竟多少词汇才够用?
  20. 趣发现 资源页面请求链接分析

热门文章

  1. 微信公众账号 token 验证失败 解决办法
  2. 写个买卖小游戏,第1天(昨天)
  3. js exec方法详解
  4. 配置EditPlus
  5. 机器学习之--数据构造,函数图显示
  6. xshess 要继续使用此程序,您必须应用最新的更新
  7. 驱动开发中的常用操作
  8. limit offset
  9. EXCEL解析之终极方法WorkbookFactory
  10. dedecms如何快速删除跳转的文章(记得清空内容回收站)