您的位置:首页 > 移动开发 > IOS开发

【读书笔记】iOS-OCUnit-单元测试

2018-01-03 09:58 453 查看
一,新建立一个hello工程---》在左侧会看到helloTests---->helloTests.m.如下图所示。

 



 

二,打开查看会看到如下代码。

 

#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>

@interface helloTests : XCTestCase

@end

@implementation helloTests

- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
}

- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}

- (void)testExample {
// This is an example of a functional test case.
XCTAssert(YES, @"Pass");
}

- (void)testPerformanceExample {
// This is an example of a performance test case.
[self measureBlock:^{
// Put the code you want to measure the time of here.
}];
}

@end


 

 三,里面函数的说明。

     1. -(void)setUp: 每个test方法执行前调用,用于类的创建,一些变量的初始化等

     2. -(void)tearDown:每个test方法执行后调用

     3. -(void)testXXX:这个是真正运行的测试方法,命名就是不带参数,以test开始。

 

四,Product-->Test--》运行测试。输出结果如下图所示:

Test Suite 'All tests' started at 2016-01-23 05:08:13 +0000
Test Suite 'helloTests.xctest' started at 2016-01-23 05:08:13 +0000
Test Suite 'helloTests' started at 2016-01-23 05:08:13 +0000
Test Case '-[helloTests testExample]' started.
Test Case '-[helloTests testExample]' passed (0.000 seconds).
Test Case '-[helloTests testPerformanceExample]' started.
/Users/chenlihua/Desktop/hello/helloTests/helloTests.m:35: Test Case '-[helloTests testPerformanceExample]' measured [Time, seconds] average: 0.000, relative standard deviation: 121.431%, values: [0.000002, 0.000001, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100
Test Case '-[helloTests testPerformanceExample]' passed (0.288 seconds).
Test Suite 'helloTests' passed at 2016-01-23 05:08:13 +0000.
Executed 2 tests, with 0 failures (0 unexpected) in 0.288 (0.289) seconds
Test Suite 'helloTests.xctest' passed at 2016-01-23 05:08:13 +0000.
Executed 2 tests, with 0 failures (0 unexpected) in 0.288 (0.290) seconds
Test Suite 'All tests' passed at 2016-01-23 05:08:13 +0000.
Executed 2 tests, with 0 failures (0 unexpected) in 0.288 (0.291) seconds


 

参考资料:《iOS测试指南》
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: