iOS 单元测试

参考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 **




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值