Mocha测试接口类型

本文介绍了Mocha测试框架的多种接口风格,包括BDD和TDD模式,以及exports方式组织测试用例。详细讨论了suite()、test()、suiteSetup()、suiteTeardown()、setup()和teardown()等钩子函数的使用,强调了在TDD模式下如何编写测试用例,并指出每个内层测试用例在执行前会调用外层的setup()函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参数

之前我们写 Mocha测试用例的时候,主要用 describe(), it() 组织用例。这跟 Jasmine 风格是类似的。实际上,这只是 Mocha 支持的一种而已。

在命令行中,有如下命令可供我们选择。

-u, --ui <name>  specify user-interface (bdd|tdd|qunit|exports)
默认情况下,Mocha使用 bdd 帮我们执行 describe(), it() 的用例。

describe('#indexOf()', function(){
	it('should return -1 when not present', function(){
      [1,2,3].indexOf(4).should.equal(-1);
    });
});
我们也可以选择其他接口类型。比如 tdd。
Mocha的测试接口类型指的是集中测试用例组织模式的选择,更像是为用户提供几种组织测试用例的方式。

BDD

BDD行为驱动开发,全名 Behavior Driven Development。
Mocha默认的测试接口。 BDD测试接口提供 describe(), context(), it(), specify(), before(), after(), beforeEach(), 和 afterEach()几种函数,其中context函数只是describe函数的别名,specify函数也是it函数的别名。

另外,Jasmine的测试风格就是bdd,它的特征就是使用describe,it。 对 BDD 有兴趣的可以去这个网站看下
http://jbehave.org/introduction.html

TDD

TDD,测试驱动开发(Test-Driven Development)
TDD接口提供 suite(), test(), suiteSetup(), suiteTeardown(), setup(), 和 teardown()函数,用例写法如下:

	suite('#indexOf()', function(){
        test('should return -1 when not present', function(){
            ([1,2,3].indexOf(4)).should.be.eql(-1);
        });
    });
tdd跟bdd区别在于,它使用suite,test,suiteSetup,suiteTeardown,setup,teardown 等函数。


Exports

exports 跟 node 里的模块语法很像,before, after, beforeEach, and afterEach 都是作为对象的属性,其它对象的值默认是 suite,属性是函数的话,代表是一个test。简单来说,除了钩子函数,对象是 测试集, 属性是 测试用例。

require("should");
module.exports = {
      before: function(){
            // ...
      },

      'Array': {
            '#indexOf()': {
              'should return -1 when not present': function(){
                [1,2,3].indexOf(4).should.equal(-1);
              }
            }
      }
};

钩子函数

从 BDD 到 TDD,describe 和 it 变变成了 suite, test。对应的钩子函数也变了。那它们的行为有没有改变呢?下面是个例子。

var assert = require('assert');
var mocha  = require('mocha');
require("should");

suite('Array', function(){
	suiteSetup(function(){
		console.log();
		console.log('-----------suiteSetup');
    });
    suiteTeardown(function(){
		console.log('-----------suiteTeardown');
		console.log();
    });
    setup(function(){
    	console.log();
    	console.log('-----------setup');
    });
    teardown(function(){
    	console.log('-----------teardown');
    	console.log();
    });
    test('First layer', function(){
    	([1,2,3].indexOf(4)).should.eql(-1);
    });
    suite('Second layer', function(){
    	suiteSetup(function(){
			console.log();
			console.log('-----------Second layer suiteSetup');
	    });
	    setup(function(){
	    	console.log();
	    	console.log('-----------Second layer setup');
	    });
    	test('Second layer test', function(){
    		([1,2,3].indexOf(4)).should.eql(-1);
    	});
    });
});
输出


从上面可以看出,每个内层测试用例执行前会执行外层的 setup, 这点跟 bdd 是一样的。至于其他的,有兴趣的话可以自己试下。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值