titanium alloy单元测试

如何在titanium下进行单元测试:

请看这片文章

如何在alloy中进行单元测试:

跟titanium中进行单元测试差不多,不过目录要变一下

1.将qunit框架代码放到assets/qunit文件夹中

2.将测试模块放到assest/test文件夹中

titanium alloy单元测试_第1张图片

3.更改下其中代码的导入路径+优化(为了方便直接上代码了,谁想用直接复制)

1)qunit/qunit.js

https://github.com/lukaso/qunit/blob/master/qunit/qunit.js

2)qunit/titanium_adaptor.js

Titanium.include('qunit.js');

// =============================================================================
// Uncomment the following lines in order to get jsMockito support for mocking 
// (after following jsMockito install instructions)
// =============================================================================

// Titanium.include('qunit/jshamcrest.js');
// Titanium.include('qunit/jsmockito-1.0.2.js');
// JsHamcrest.Integration.QUnit();
// JsMockito.Integration.importTo(this);

var logger = function(failures, message) {
	if (failures) {
		Titanium.API.error(message);		
	} else {
		Titanium.API.info(message);
	}
};
// QUnit.testStart(name) is called whenever a new test batch of assertions starts running. name is the string name of the test batch.
QUnit.testStart = function(name) {
	logger(false, '>> >> >>TEST START: '+name);
};
// QUnit.testDone(name, failures, total) is called whenever a batch of assertions finishes running. name is the string name of the test batch. failures is the number of test failures that occurred. total is the total number of test assertions that occurred.
QUnit.testDone = function(name, failures, total) {
	logger(failures, '<< << <> >>MODULE START: '+name);
};
// QUnit.moduleDone(name, failures, total) is called whenever a module finishes running. name is the string name of the module. failures is the number of module failures that occurred. total is the total number of module assertions that occurred.
QUnit.moduleDone = function(name, failures, total) {
	logger(failures, '<< <
3)test/tests_to_run.js

//判断是否引入qunit框架,如果没引入则引入
if(!test){
	var test = {};
	Ti.include("../qunit/titanium_adaptor.js");
}
//必然通过的测试
test("test true", function() {
	ok(true, "hello");
});
//必然失败的测试
test("test false", function() {
	ok(false, "world");
});
4.剩下的直接在控制器下导入进来就可以了

Ti.include("/test/tests_to_run.js");//assest文件夹下的资源都用"/"+资源名表示



你可能感兴趣的:(这叫titanium)