ǰÑÔ£º²âÊÔÊÇÒ»¸öºÃµÄApp²»¿ÉȱÉٵIJ¿·Ö¡£Ã¿Ò»¸öApp¶¼ÊÇÓÉÒ»¸ö¸öСµÄ¹¦ÄÜ×éºÏµ½Ò»ÆðµÄ¡£¶øÕâЩСµÄ¹¦ÄÜÓÖÊÇÓÉÒ»¸ö¸öº¯Êý»òÕß˵Ëã·¨×éºÏµ½Ò»ÆðµÄ¡£µ¥Ôª²âÊÔ¾ÍÊǶÔÕâЩСµÄ¹¦ÄÜ»òÕߺ¯Êý½øÐвâÊÔ£¬Á¼ºÃµÄµ¥Ôª²âÊÔ»áÈôúÂëµÄ½¡×³ÐÔÌá¸ßºÜ¶à¡£XCTest¾ÍÊÇXCodeΪÎÒÃÇÌṩµÄÒ»¸ö¿ò¼Ü£¬ËüÌṩÁ˸÷¸ö²ã´ÎµÄ²âÊÔ¡£
XCTestCase
ÿ¸öXCode´´½¨iosµÄ¹¤³ÌÖж¼ÓÐÒ»¸ö½Ð×ö¡±¹¤³ÌÃûTests¡±µÄ·Ö×飬Õâ¸ö·Ö×éÀï¾ÍÊÇXCTestCaseµÄ×ÓÀ࣬XCTestÖеIJâÊÔÀà¶¼ÊǼ̳Ð×ÔXCTestCase¡£
ÀýÈçн¨Ò»¸ö¹¤³Ì£¬ÃüÃûΪDemo£¬¾ÍÄÜ¿´µ½Èçͼ

¿´Ò»ÏÂÕâ¸ö×Ô¶¯´´½¨µÄÎļþÀï¶¼°üº¬ÁËÄÄЩÄÚÈÝ
#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
@interface DemoTests : XCTestCase
@end
@implementation DemoTests
- (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 |
²âÊÔÓÃÀýµÄÃüÃû
XCTestÖÐËùÓеIJâÊÔÓÃÀýµÄÃüÃû¶¼ÊÇÒÔtest¿ªÍ·µÄ¡£ÀýÈçÉÏÎÄÖеÄ
- (void)testExample
{
// This is an example of a functional test case.
XCTAssert(YES, @"Pass");
} |
setUpºÍtearDown
SetupÊÇÔÚËùÓвâÊÔÓÃÀýÔËÐÐ֮ǰÔËÐеĺ¯Êý£¬ÔÚÕâ¸ö²âÊÔÓÃÀýÀï½øÐÐһЩͨÓõijõʼ»¯¹¤×÷
tearDownÊÇÔÚËùÓеIJâÊÔÓÃÀý¶¼Ö´ÐÐÍê±ÏºóÖ´ÐеÄ
XCodeµÄ²âÊÔÓÃÀýµ¼º½
²âÊÔÓÃÀýµÄµ¼º½Èçͼ£¬ÔÚ²âÊÔÓÃÀýµÄµ¼º½ÀÎÒÃÇ¿ÉÒÔÔËÐÐÒ»×é²âÊÔÓÃÀý£¬Ò²¿ÉÒÔÔËÐÐÒ»¸öµ¥¶ÀµÄ²âÊÔÓÃÀý

¿ÉÒÔÊó±êÓÒ¼üÀ´Ð½¨Ò»×é²âÊÔÓÃÀý¡£

Ò²¿ÉÒÔΪ²âÊÔÓÃÀýÌí¼Óʧ°Ü¶ÏµãÀ´·½±ãÎÒÃǵ÷ÊÔ

²é¿´²âÊÔ½á¹û
ͨ¹ý²âÊÔµ¼º½À¸¿ÉÒԲ鿴µ½²âÊÔ½á¹û

ͨ¹ýReportµ¼º½À¸¿ÉÒÔ¿´µ½¸üÏêϸµÄ²âÊÔ½á¹û

µã»÷²âÊÔÓÃÀýºóÃæµÄ¼ýÍ·£¬¿ÉÒÔÌø×ªµ½²âÊÔÓÃÀýµÄ´úÂë¡£
ÆÕͨ·½·¨²âÊÔ
ÀýÈ磬н¨Ò»¸öÀàÃüÃûΪModel£¬ËûÓÐÕâ¸ö·½·¨ÓÃÀ´Éú³É10ÒÔÄÚµÄËæ»úÊý¡£
-(NSInteger)randomLessThanTen{
return arc4random()%10;
} |
ÓÚÊÇ£¬²âÊÔ·½·¨Îª
-(void)testModelFunc_randomLessThanTen{
Model * model = [[Model alloc] init];
NSInteger num = [model randomLessThanTen];
XCTAssert(num<10,@"num should less than
10");
} |
ÎÒÃǵã»÷ÈçͼµÄ×ó±ßͼ±êµ¥¶ÀÔËÐÐÕâ¸ö²âÊÔÓÃÀý£¬µ±È»Ò²¿ÉÒÔÔÚÉÏÎÄÎÒÌáµ½µÄµ¼º½À¸Àïµ¥¶ÀÔËÐС£

È»ºó»á¿´µ½Êä³ö±íʾÕâ¸ö²âÊÔÓÃÀýͨ¹ý
Test Suite 'Selected
tests' started at 2015-06-28 05:24:56 +0000
Test Suite 'DemoTests.xctest' started at 2015-06-28
05:24:56 +0000
Test Suite 'DemoTests' started at 2015-06-28 05:24:56
+0000
Test Case '-[DemoTests testModelFunc_randomLessThanTen]'
started.
Test Case '-[DemoTests testModelFunc_randomLessThanTen]'
passed (0.000 seconds).
Test Suite 'DemoTests' passed at 2015-06-28 05:24:56
+0000.
Executed 1 test, with 0 failures (0 unexpected)
in 0.000 (0.001) seconds
Test Suite 'DemoTests.xctest' passed at 2015-06-28
05:24:56 +0000.
Executed 1 test, with 0 failures (0 unexpected)
in 0.000 (0.001) seconds
Test Suite 'Selected tests' passed at 2015-06-28
05:24:56 +0000. |
³£ÓöÏÑÔ
ÈçºÎÅжÏÒ»¸ö²âÊÔÓÃÀý³É¹¦»òÕßʧ°ÜÄØ£¿XCTestʹÓöÏÑÔÀ´ÊµÏÖ¡£
×î»ù±¾µÄ¶ÏÑÔ
±íʾÈç¹ûexpressionÂú×㣬Ôò²âÊÔͨ¹ý£¬·ñÔò¶ÔÓ¦formatµÄ´íÎó¡£
XCTAssert(expression,
format...) |
»¹ÓÐÒ»¸öÓÃÀ´Ö±½ÓFailµÄ¶ÏÑÔ
ÆäËûһЩ³£ÓõĶÏÑÔ£º
XCTAssertTrue(expression,
format...)
XCTAssertFalse(expression, format...)
XCTAssertEqual(expression1, expression2, format...)
XCTAssertNotEqual(expression1, expression2, format...)
XCTAssertEqualWithAccuracy(expression1, expression2,
accuracy, format...)
XCTAssertNotEqualWithAccuracy(expression1, expression2,
accuracy, format...)
XCTAssertNil(expression, format...)
XCTAssertNotNil(expression, format...) |
ÐÔÄܲâÊÔ
ËùνÐÔÄܲâÊÔ£¬Ö÷Òª¾ÍÊÇÆÀ¹ÀÒ»¶Î´úÂëµÄÔËÐÐʱ¼ä£¬XCTestµÄÐÔÄܵIJâÊÔÀûÓÃÈçϸñʽ
¶ÔÓÚÐÔÄܲâÊÔ£¬Ã¿Ò»¸ö²âÊÔÓÃÀýÿ´Î»áÔËÐÐ10´Î¡£
- (void)testPerformanceExample
{
// This is an example of a performance test case.
[self measureBlock:^{
// Put the code you want to measure the time of
here.
}];
} |
ÀýÈ磬ÎÒÒªÆÀ¹ÀÒ»¶Î´úÂ룬ѻ·´òÓ¡NSLog 10000´Î¡£
Õâ¶Î´úÂëÈçÏ£¬Õâ¶Î´úÂëÎÒ·ÅÔÚUIImageµÄÀà±ðÀï¡£
- (void)testPerformanceExample
{
// This is an example of a performance test case.
[self measureBlock:^{
for (NSInteger index = 0; index < 10000; index
++) {
NSLog(@"%ld",index);
}
// Put the code you want to measure the time of
here.
}];
} |
ÎÒÃǶ¼ÖªµÀ£¬²âÊÔҪô³É¹¦£¬ÒªÃ´Ê§°Ü£¬ÄÇô¾ÍÒýÈëÁËÒ»¸ö¹Ø¼üµÄÎÊÌâ
ÐÔÄܲâÊÔµÄʱºò£¬ÈçºÎÅÐÒ»¸öÐÔÄܲâÊÔcaseÊdzɹ¦»¹ÊÇʧ°ÜÄØ£¿
ÎÒÃÇÏÈͨ¹ýÉÏÎĵķ½Ê½£¬Ö»ÔËÐÐÒ»´ÎÕâ¸ö²âÊÔÓÃÀý¡£È»ºó¿´¿´½á¹ûºÍÊä³ö£¨Õâ¸ö²âÊÔÓÃÀýÅܵĺÜÂý£¬±ð׿±£©
Test Case '-[ModelTests
testPerformanceExample]' failed (37.432 seconds).
Test Suite 'ModelTests' failed at 2017-02-19 09:57:26.210.
Executed 1 test, with 1 failure (0 unexpected)
in 37.432 (37.433) seconds
Test Suite 'ToDoTests.xctest' failed at 2017-02-19
09:57:26.211.
Executed 1 test, with 1 failure (0 unexpected)
in 37.432 (37.434) seconds
Test Suite 'Selected tests' failed at 2017-02-19
09:57:26.211.
Executed 1 test, with 1 failure (0 unexpected)
in 37.432 (37.437) seconds
Test session log:
/Users/hl/Library/Developer/Xcode/DerivedData/ToDo-bbcdkwvzbmyznocgystdcavfakca/Logs/Test/98E0FA82-BACC-4361-AF39-E0734F73A545/Session-ToDoTests-2017-02-19_095641-jm2eKF.log |
È»ºó£¬Äã»á·¢ÏÖ²âÊÔʧ°ÜÁË£¡ÕâÊÇÒòΪÎÒÃÇûÓиøÐÔÄܲâÊÔÒ»¸ö²Î¿¼Ê±¼ä¡£
ÎÒÃǵã»÷ͼÖеĵĵڶþ¸ö²æ¼ýÍ·

È»ºó£¬¿´µ½Èçͼ

ÎÒÃÇÀ´¿´¿´Õ⼸¸ö²ÎÊý¶¼ÊÇɶÒâ˼£º
Baseline ¼ÆËã±ê×¼²îµÄ²Î¿¼Öµ
MAX STDD ×î´óÔÊÐíµÄ±ê×¼²î
µ×²¿µã»÷1£¬2¡10¿ÉÒÔ¿´µ½Ã¿´ÎÔËÐеĽá¹û¡£
µã»÷Edit£¬ÎÒÃǵã»÷AverageµÄÓұߵÄAccept£¬À´Èñ¾´ÎÔËÐÐµÄÆ½¾ùÖµÉèÖÃΪbaseline£¬È»ºóÈ»ºóMAX
STDD¸ÄΪ40%¡£ÔÙÔËÐÐÕâ¸ö²âÊÔÓÃÀý£¬Äã»á·¢ÏÖ²âÊԳɹ¦ÁË¡£
Òì²½²âÊÔ
Òì²½²âÊÔµÄÂß¼ÈçÏ£¬Ê×Ïȶ¨ÒåÒ»¸ö»òÕß¶à¸öXCTestExpectation£¬±íʾÒì²½²âÊÔÏëÒªµÄ½á¹û¡£È»ºóÉèÖÃtimeout£¬±íʾÒì²½²âÊÔ×î¶à¿ÉÒÔÖ´ÐеÄʱ¼ä¡£×îºó£¬ÔÚÒì²½µÄ´úÂëÍê³ÉµÄ×îºó£¬µ÷ÓÃfullfillÀ´Í¨ÖªÒì²½²âÊÔÂú×ãÌõ¼þ¡£
- (void)testAsyncFunction{
XCTestExpectation * expectation = [self expectationWithDescription:@"Just
a demo expectation,should pass"];
//Async function when finished call [expectation
fullfill]
[self waitForExpectationsWithTimeout:10 handler:^(NSError
*error) {
//Do something when time out
}];
} |
¾ÙÀý
- (void)testAsyncFunction{
XCTestExpectation * expectation = [self expectationWithDescription:@"Just
a demo expectation,should pass"];
dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE _PRIORITY_DEFAULT,
0), ^{
sleep(1);
NSLog(@"Async test");
XCTAssert(YES,"should pass");
[expectation fulfill];
});
[self waitForExpectationsWithTimeout: 10 handler:^(NSError
*error) {
//Do something when time out
}];
} |
²âÊÔ½á¹û
Test Suite 'Selected
tests' started at 2015-06-28 05:49:43 +0000
Test Suite 'DemoTests.xctest' started at 2015-06-28
05:49:43 +0000
Test Suite 'DemoTests' started at 2015-06-28 05:49:43
+0000
Test Case '-[DemoTests testAsyncFunction]' started.
2015-06-28 13:49:44.920 Demo[2157:145428] Async
test
Test Case '-[DemoTests testAsyncFunction]' passed
(1.006 seconds).
Test Suite 'DemoTests' passed at 2015-06-28 05:49:44
+0000.
Executed 1 test, with 0 failures (0 unexpected)
in 1.006 (1.007) seconds
Test Suite 'DemoTests.xctest' passed at 2015-06-28
05:49:44 +0000.
Executed 1 test, with 0 failures (0 unexpected)
in 1.006 (1.009) seconds
Test Suite 'Selected tests' passed at 2015-06-28
05:49:44 +0000. |
´úÂ븲¸ÇÂÊ
Ñ¡ÔñTarget£¬È»ºóÑ¡ÔñTestÄ£¿é£¬È»ºó¹´Ñ¡Gather coverage data

È»ºó£¬ÔÚreportÄ£¿éÖУ¬¾ÍÄÜ¿´µ½Ã¿Ò»¸ö.mÎļþµÄ´úÂ븲¸ÇÇé¿öÁË¡£

|