js断言测试

更新日期: 2019-06-23阅读: 3.6k标签: 测试

断言一词来自逻辑学,在逻辑学中,断言是断定一个特定前提为真的陈述,在软件测试中也是类似的含义。测试中断言语句的一般形式为assert 表达式,其中的表达式就是逻辑学中的陈述,表达式的值为真(true)的时候该断言才能通过,否则就断言失败。

JS的生态系统中有很多断言库如:

断言框架很多,好消息是上述的断言框架都能够引入到mocha测试框架上使用。下面我们就来看看这些断言框架的测试都是什么风格。


Node.js assert模块

介绍

Node.js的assert模块下定义了一系列assert相关函数,如assert.ok(),assert.equal, assert.fail, assert.deepEqual, assert.deepStrictEqual, assert.doesNotThrow, assert.ifError, assert.notDeepEqual, assert.notDeepStrictEqual, assert.notEqual, assert.notStrictEqual, assert.strictEqual, assert.throws等方法。

当断言不为真时,这些函数都会抛出js异常。 如定义index.js文件如:

const assert = require('assert');
assert.strictEqual(1, '1');

运行node index.js会抛出异常:

assert.js:90
  throw new assert.AssertionError({
  ^
AssertionError: 1 === '1'
......

mocha中使用

const assert = require('assert');

describe('nodejs assert module', () => {
  it('strict equal assertion will fail', () => {
    assert.strictEqual(1, '1');
  });

  it('throw error assertion will pass', () => {
    assert.throws(
      () => {
        throw new Error('Wrong value');
      },
      /value/
    );
  });
});


better-assert

介绍

npm install -D better-assert 来安装。

C语言风格的断言如下index.js:

const assert = require('better-assert');

assert(1 == '1');
assert(1 === '1');

运行node index.js,将抛出异常:

throw err;
  ^
AssertionError: 1 === '1'
......

mocha中使用

const assert = require('better-assert');

describe('better-assert', () => {
  it('will assert fail on the strict equal expression', () => {
    assert(1 === '1');
  });
});


shoud.js

介绍

npm install -D should 来安装。

此库的目的是The main goals of this library are to be expressive and to be helpful.. 这个库使得所写的测试语义化,其功能非常强大。

默认的,当你调用require('should')时,其将扩展Object.prototype, 此时,就可以在继承自Object(JS中所有对象的基类)的对象上使用should方法,如

(1).should.be.exactly(1).and.be.a.Number();
({ name : 'paul' }).should.have.property('name').which.is.a.String()
"abc".should.be.eql('abc');
false.should.not.be.ok();
(new Promise(function(resolve, reject) { resolve(10); })).should.be.a.Promise()

如果不希望扩展Object.prototype,可以const should = require('should/as-function'), 而后使用

should(1).be.exactly(1);
shoud(true).be.ok();

创建index.js如:

const should = require('should');
(1).should.be.exactly('1');

node index.js将抛出异常:

throw new AssertionError(params);      ^
AssertionError: expected 1 to be '1'
.....

should.js还支持在浏览器上运行,只需要引入浏览器上使用的should.js或自己构建最新的should.js,并使用script tag将其引入html中, 就可以在window对象中获取should方法。  

should.js定义了许多有用的函数,参考shouldjs文档

be,and,a,have,is,it 这些只是简单的链或者叫别名,反正都是 be 的别名而已,文档里有说明的

汉化should.js?

var should = require('should');
var Assertion = should.Assertion;

should.extend('应该', Object.prototype); // should 别名

['是', '个', '并且'].forEach(function(name) { // 属性别名
    Assertion.addChain(name);
});

// 方法别名
Assertion.alias('not', '不');
Assertion.alias('equal', '等于');
Assertion.alias('Number', '数字');
Assertion.alias('String', '字符串');

// 测试例子
(5).应该.等于(5).并且.是.个.数字();
'5'.应该.等于('5').并且.是.个.字符串();
(5).应该.等于(5).并且.不.是.个.数字();

console.log('完成!');

mocha中使用

describe('should', function() {
  it('test customize test', function() {
     (1+1).should.be.equal(2);
  });
});


expect.js

介绍

Expect.js 库应用十分广泛,它拥有很好的仿自然语言的方法。其实基于should.js编写的一个断言库。

npm install -D expect.js

创建expect.js如:

const expect = require('expect.js');

expect({ a: 'b' }).to.eql({ a: 'b' })
expect([]).to.be.an('array');
expect(5).to.be.a(Number);
expect(1).to.eql('1');
expect('1.0.1').to.match(/[0-9]+\.[0-9]+\.[0-9]+/);
expect([1, 2]).to.contain(1);
expect('hello world').to.contain('world');
expect([1,2,3]).to.have.length(3);
expect([]).to.be.empty();
expect({ my: 'object' }).to.not.be.empty();
expect({a: 'b'}).to.have.property('a');
expect({ a: 'b', c: 'd' }).to.only.have.keys('a', 'c');
expect({ a: 'b', c: 'd' }).to.only.have.keys(['a', 'c']);
expect( (a,b) => a+b) ).to.not.throwException();
expect( (a,b) => a+b) ).withArgs(1,1).to.not.throwException();

node expect.js即可以看到测试的结果,如果断言出错,会抛出异常。

  • equal表示严格等于 ===

  • eql不严格的等于,可以用来做判断对象是否相等

  • match用来做正则匹配

  • contain用来判断是否在数组或字符串中包含

  • length用来断言数组长度

  • empty来判断数组或对象不为空

  • property来判断对象是否包含某个属性/key

  • key/keys支持判断对象是否包含某个或某些key, 可以与only修饰符一起用

  • throwException/throwError来判断某个函数是否抛出异常,注意是函数指针,而不是函数调用

  • withArgs来可以测试函数以及传递不同参数的情况下返回的值

expect.js也可以很容易在浏览器中运行,只需要加入script tag:

<script src="expect.js"></script>

mocha中使用

const expect = require('expect.js');

describe('expect.js', function() {
  it('expectjs test', function(done) {
    expect({ a: 'b' }).to.eql({ a: 'b' })
  });
});


unexpected.js

介绍

npm install -D unexpected

unexpected使用了一种与should类似但不同的断言语法, 如:

expect({ text: 'f00!' }, 'to equal', { text: 'f00!' });
expect((a,b) => a+b, 'to be a', 'function');
expect(1+1, 'to be', 2);

mocha中使用

const expect = require('unexpected');

describe('unexpected', function() {
  it('test', function() {
    expect({ text: 'f00!' }, 'to equal', { text: 'f00!' });
  });
});


Chai

介绍

chai即支持BDD风格的测试写法(should, expect), 也支持TDD风格的写法(assert.equal);

  • 使用should时测试前调用 chai.should();

  • 获取expect对象 var expect = chai.expect;

  • 获取assert对象 const assert = chai.assert

chai的语法可以参考chai API文档

chai之强大不仅仅在于其支持多种形式的断言写法,其还支持很多插件在其上的使用,如chai-as-promised, chai-sinon等。

mocha中使用

const chai = require('chai');

chai.should();
const expect = chai.expect;
const assert = chai.assert;

describe.only('chai', function() {
  it('test with should', function() {
    (1).should.be.exactly(1);
  });

  it('test with expect', function() {
    expect(1+1).to.equal(2);
  });

  it('test with assert', function() {
    assert.equal(1+1, 2);
  });
});

链接: https://www.fly63.com/article/detial/3847

测试工具比较:选Jest,不选Mocha

Jest的未来看起来非常令人激动!看到Jest推陈出新如此快速,我感觉它将很快成为整个React生态系统中大部分项目的首选工具。我建议,应该把测试迁移到Jest上去。

你需要了解的前端测试“金字塔”

如果您正在测试前端应用程序,则应该了解前端测试金字塔。在本文中,我们将看到前端测试金字塔是什么,以及如何使用它来创建全面的测试套件。

web网页性能测试工具都有哪些

作为前端开发,我们不仅需要满足产品需求功能的实现,同时也需要对自己做的网站进行安全、易用性、性能等方面的考虑。随着目前技术不断进步,web页面的性能测试工具也在不断完善,通过这些工具,我们可以客观的评价web网站的质量水平。

js单元测试工具-jest自动化测试

jest 是 facebook 开源的,用来进行单元测试的框架,可以测试 javascipt 和 react。jest 提供了非常方便的 API,可以对下面的场景方便的测试:一般函数、异步函数、测试的生命周期、react 测试

web测试要点、方法_web端测试大全总结

web测试大全,测试web网站有哪些点呢?主要包括:功能测试、兼容性测试、安全测试、输入框测试、用户权限测试等

前端性能测试工具整理简介_性能测试工具都有哪些?

前端性能测试工具都有哪些:Favicon、Open Graph、图片优化-压缩图像、CSS 优化-Autoprefixer、Purifycss、minify CSS、减少载入时间、GZIP、CDN、优化平台-Sentry、Google Tag Manager

不用写代码,也能做好接口测试

本文你将了解到:1、接口测试基本概念,包含什么是接口,什么是接口测试,为什么要做接口测试;2、接口测试用例设计,3、怎样不用写代码,也能快速的根据开发的API文档完成接口自动化测试脚本

Selenium打开浏览器加载慢的原因

在自动化元素定位操作中经常使用智能等待来加强定位的强壮性,主要就是因为WebDriver没有提供页面加载场景的方法;在使用JavaScript知识的突然心生灵感,可以使用JavaScript来配合验证页面加载,结果发现我真是井底之蛙。

power assert_更智能、优雅的全方位 assert 断言库

在写测试代码时,以往我们需要翻阅文档,学习各种 API 才能明白如何操作断言。而现在我们可以透过 power-assert 的 assert 方法来减轻调试压力。不仅如此,它还提供更加直观,具体的运行效果,帮助 DEBUG。写测试代码,其实可以很容易。

常用的web网站负载/压力/性能测试工具

在网站上线发布之前,我们除了必要的安全、功能测试外,往往还需要进行压力测试。通过模拟实际应用的软硬件环境及用户使用过程的系统负荷,长时间或超大负荷地运行测试软件。包括:Apache JMeter 、LoadRunner、NeoLoad等

点击更多...

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!