iOS测试
Xcode中Instruments的使用:
Leaks:
Leaks可以检测内存泄漏点,Allocations跟踪模板可以查看内存的使用情况。在Instruments中,虽然选择了Leaks模板,但默认情况下也会添加Allocations模板。基本上凡是分析内存都会使用Allocations模板,它可以监控内存分布情况。双击错误位置会直接找到错误代码位置。
使用方法参考:http://cn.cocos2d-x.org/tutorial/show?id=1837
Automation:
测试UI的工具。在录制完成后修改JavaScript代码,循环执行某个动作或UI界面根据报错进行分析。双击错误,到代码错误位置进行查看。
简单的JS登录脚本:
var target = UIATarget.localTarget();
var app = target.frontMostApp();
var mainWindow = app.mainWindow();
target.logElementTree();
var userTextField = mainWindow.textFields()[0];
userTextField.setValue("admin");
mainWindow.secureTextFields()[0].setValue("admin");
var button = mainWindow.buttons()["登 录"];
button.tap(); //点击button
Automation具有录制功能,点击录制后打开模拟器进行操作,Automation会自动生成JS代码,我们只需要在我们需要测试的模块加上For循环或者其他你想要的实现测试的方式即可。
使用方法参考:http://www.th7.cn/Program/IOS/201404/192097.shtml
XCTest:
1.异步测试
-(void)testHttpRequest{
// 创建期望(相当于一个NSRunLoop,因为我们不知道异步什么时候执行结束)
XCTestExpectation *expectation = [self expectationWithDescription:@"Handler called"];
myClass = [[MyClass alloc]init];
[myClass postWithUrl:@"http://baidu.com" callBackResult:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// expection调用已完成
[expectation fulfill];
XCTAssertEqualObjects(response, @"bar");
//判断登录的时候在这里解析数据,使用断言判断登录是否成功
}];
//运行循环在处理事件,直到所有的期望都满足或超时
[self waitForExpectationsWithTimeout:5 handler:nil];
}
2.XCTest中有 measureBlock:方法进行对算法或者某一部分的性能进行测试,如下:
-(void)testPerformance{
// 测试代码性能的一个block(如测试算法,读取内存等等)
[self measureBlock:^{
// set the initial state
[self.testVC pressView:[self.testVC.view viewWithTag:1000]];
// iterate for 100000 cycles of adding 2
for (int i=0; i<100000; i++) {
[self.testVC pressView:[self.testVC.view viewWithTag:1000]];
}
}];
}
3. 测试application创建了testVC的实例
-(void)testAppliation{
self.testVC = (ViewController*)[[UIApplication sharedApplication] delegate].window.rootViewController;
UIView *testView = self.testVC.view;
// 判断不为空的断言
XCTAssertNotNil(testView, @"Cannot find CalcView instance");
}
Kiwi框架测试:
1.使用cocoapods导入Kiwi框架(Kiwi导入到testtarget下) eg:
target :"TestXcodeTestTests",:exclusive => true do
platform:ios,'7.0'
pod 'Kiwi', '~> 2.3.1'
end
2.创建一个空的类,只需要.m文件就可以。删除所有代码后导入Kiwi框架
3.具体测试代码如下:
// describe 描述测试对象的内容(一般一个测试文件只描述一个类)
describe(@"SimpleString", ^{
// context描述测试的上下文
context(@"when assigned to 'Hello world'", ^{
NSString *greeting = @"Hello world";
// it描述测试的本体
it(@"should exist", ^{
// 这里封装了类似Xctest中断言的方法
[[greeting shouldNot] beNil];
});
it(@"should equal to 'Hello world'", ^{
[[greeting should] equal:@"Hello world"];
});
});
});
使用Kiwi测试tableView的datasource的方法:
SPEC_BEGIN(ArrayDataSourceSpec)
describe(@"ArrayDataSource", ^{
context(@"Initializing", ^{
it(@"should not be allowed using init", ^{
[[[[ArrayDataSource alloc] init] should] beNil];
});
});
context(@"Configuration", ^{
__block DetialTableViewCell *configuredCell = nil;
__block id configuredObject = nil;
TableViewCellConfigureBlock block = ^(DetialTableViewCell *a, id b){
configuredCell = a;
configuredObject = b;
};
ArrayDataSource *dataSource = [[ArrayDataSource alloc] initWithItems:@[@"a", @"b"] cellIdentifier:@"foo" configureCellBlock:block];
id mockTableView = [UITableView mock];
DetialTableViewCell *cell = [[DetialTableViewCell alloc] init];
__block id result = nil;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
it(@"should receive cell request", ^{
[[mockTableView should] receive:@selector(dequeueReusableCellWithIdentifier:forIndexPath:) andReturn:cell withArguments:@"foo",indexPath];
result = [dataSource tableView:mockTableView cellForRowAtIndexPath:indexPath];
});
it(@"should return the dummy cell", ^{
[[result should] equal:cell];
});
it(@"should have benn passed to the block", ^{
[[configuredCell should] equal:cell];
});
it(@"should have the same configured object", ^{
[[configuredObject should] equal:@"a"];
});
});
context(@"number of rows", ^{
id mockTableView = [UITableView mock];
ArrayDataSource *dataSource = [[ArrayDataSource alloc] initWithItems:@[@"a", @"b"] cellIdentifier:@"foo" configureCellBlock:nil];
it(@"should be 2 items", ^{
NSInteger count = [dataSource tableView:mockTableView numberOfRowsInSection:0];
[[theValue(count) should] equal:theValue(2)];
});
});
});
SPEC_END

浙公网安备 33010602011771号