参考Apple官方文档,写一个单元测试的实例。
从官方下载计算器代码:UnitTests.zip,这是一个计算器程序。
进入测试tab,可以执行工程自带的测试用例。
我们添加一个自己的测试用例SampleCalcTests:
在生成的SampleCalcTest.m中添加头文件引用和成员变量:
#import "iOS_CalcAppDelegate.h"
#import "iOS_CalcViewController.h"
@interface SampleCalcTests : XCTestCase {
@private
UIApplication *app;
CalcAppDelegate *appDelegate;
iOS_CalcViewController *calcViewController;
UIView *calcView;
}
@end
修改setUp方法,在其中初始化app、calcViewController和calcView:
- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
app = [UIApplication sharedApplication];
calcViewController = (iOS_CalcViewController *)app.delegate;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
calcViewController = [storyboard instantiateViewControllerWithIdentifier:@"CalcViewController"];
calcView = calcViewController.view;
}
添加testAddition方法,测试+操作的正确性:
- (void)testAddition
{
[calcViewController press:[calcView viewWithTag: 6]];
[calcViewController press:[calcView viewWithTag:13]];
[calcViewController press:[calcView viewWithTag: 2]];
[calcViewController press:[calcView viewWithTag:12]];
XCTAssertEqualObjects([calcViewController.displayField text], @"8", @"Part 1 failed.");
}
编译运行测试用例,通过!
- (void)testAddition
{
[calcViewController press:[calcView viewWithTag: 6]];
[calcViewController press:[calcView viewWithTag:13]];
[calcViewController press:[calcView viewWithTag: 2]];
[calcViewController press:[calcView viewWithTag:12]];
XCTAssertEqualObjects([calcViewController.displayField text], @"8", @"Part 1 failed.");
}
为了测试能够自动化,接下来我们为单元测试用例添加scheme:
这样我们就可以通过命令行执行测试用例了:
xcodebuild test -project iOS_Calc.xcodeproj -scheme SampleCalcTests -destination 'platform=iOS Simulator,name=iPhone 4s,OS=8.1'
命令执行结果如下:
Test Suite 'All tests' started at 2015-03-09 13:03:31 +0000
Test Suite 'SampleCalcTests.xctest' started at 2015-03-09 13:03:31 +0000
Test Suite 'MyAppTests' started at 2015-03-09 13:03:31 +0000
Test Case '-[MyAppTests testExample]' started.
Test Case '-[MyAppTests testExample]' passed (0.000 seconds).
Test Case '-[MyAppTests testPerformanceExample]' started.
/Users/yaming/Desktop/Dev/UnitTest/UnitTests/iOS_Calc/SampleCalcTests/MyAppTests.m:35: Test Case '-[MyAppTests testPerformanceExample]' measured [Time, seconds] average: 0.000, relative standard deviation: 96.762%, 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 '-[MyAppTests testPerformanceExample]' passed (0.305 seconds).
Test Suite 'MyAppTests' passed at 2015-03-09 13:03:32 +0000.
Executed 2 tests, with 0 failures (0 unexpected) in 0.305 (0.306) seconds
Test Suite 'SampleCalcTests' started at 2015-03-09 13:03:32 +0000
Test Case '-[SampleCalcTests testAddition]' started.
Test Case '-[SampleCalcTests testAddition]' passed (0.002 seconds).
Test Case '-[SampleCalcTests testExample]' started.
Test Case '-[SampleCalcTests testExample]' passed (0.001 seconds).
Test Case '-[SampleCalcTests testPerformanceExample]' started.
/Users/yaming/Desktop/Dev/UnitTest/UnitTests/iOS_Calc/SampleCalcTests/SampleCalcTests.m:58: Test Case '-[SampleCalcTests testPerformanceExample]' measured [Time, seconds] average: 0.000, relative standard deviation: 61.739%, values: [0.000001, 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 '-[SampleCalcTests testPerformanceExample]' passed (0.254 seconds).
Test Suite 'SampleCalcTests' passed at 2015-03-09 13:03:32 +0000.
Executed 3 tests, with 0 failures (0 unexpected) in 0.258 (0.259) seconds
Test Suite 'SampleCalcTests.xctest' passed at 2015-03-09 13:03:32 +0000.
Executed 5 tests, with 0 failures (0 unexpected) in 0.563 (0.566) seconds
Test Suite 'All tests' passed at 2015-03-09 13:03:32 +0000.
Executed 5 tests, with 0 failures (0 unexpected) in 0.563 (0.567) seconds
** TEST SUCCEEDED **