ã©ã¤ãã©ãªã®ãã¹ã
ã©ã¤ãã©ãªã¯ç»é¢ãç¡ãã®ã§ãããã°ããããã。ãã¹ãç®çã®å°ç¨ã¢ããªãç¨æããã®ãããã®ã ã、éçºä¸ã¯åä½ãã¹ããå°å ¥ããã®ãæ£æ»æ³ã ãã。SDKã«ã¯åä½ãã¹ãç¨ãã¬ã¼ã ã¯ã¼ã¯ OCUnit ãæ¨æºã§ä»å±ããã®ã§ããã使ã£ã¦ã¿ã。ããã使ãã¨ãã«ãæã«ãã¹ãã±ã¼ã¹ãå®è¡ãã¦åé¡ãããã°ãããªæã㧠Xcodeã®ãã«ãçµæç»é¢ã«ã¨ã©ã¼ã表示ãã¦ããã。
æé
1.æºå
ã¾ã OCUnitæ¤è¨¼ç¨ã« Static Library éçºç¨ã®ãµã³ãã«ããã¸ã§ã¯ããæ°è¦ä½æãã。
ãã¹ã対象ã¨ãã¦ãããªã¯ã©ã¹ãç¨æãã¦ãã。
@interface SampleClass : NSObject { } - (NSString*)greeting; @end
@implementation SampleClass - (NSString*)greeting { return @"Hello"; } @endãã®ç¶æ ã§ãã«ãããã¨ã©ã¤ãã©ãª(*.a)ãä½æããã。
2. Unit Test Bundle
ãã¹ãç¨ã¿ã¼ã²ããã¨ã㦠Unit Test Bundle ã追å ãã。
ååã "Unit Test" ã¨ãã¦ã¿ã。
ãã®ã¿ã¼ã²ããã®æ å ±ãéã「ä¸è¬」ã¿ãã®「ç´æ¥ä¾åé¢ä¿」ã¸ã©ã¤ãã©ãªãã«ãç¨ã®ã¿ã¼ã²ããã追å ãã。ããããã¨ãã¹ãç¨ã¿ã¼ã²ãããå®è¡ããæ、äºåã«ã©ã¤ãã©ãªã®ãã«ããå®è¡ãã¦ããã。
ã¾ããã¹ãã±ã¼ã¹ãå®è¡ããããã°ã©ã ããã«ãããã®ã«ã©ã¤ãã©ãª(*.a)ããªã³ã¯ããå¿ è¦ãããã®ã§ãã®ã¿ã¼ã²ããã¸å ã»ã©ãã«ãããã©ã¤ãã©ãªãã¡ã¤ã«ã追å ãã。Products ã«ãã *.a ãã¡ã¤ã«ããã©ãã°&ããããã§「ãã¤ããªãã©ã¤ãã©ãªã«ãªã³ã¯」ã¸è¿½å ãããã¨ãã§ãã(ã¡ãã¥ã¼ãã追å ãã¦ãè¯ã)。
3. ãã¹ãã±ã¼ã¹ã追å ãã
æ°è¦ãã¡ã¤ã«ã¨ã㦠"Objective-C test case class" ãé¸æãã。
追å ãããã¹ãã±ã¼ã¹ã®ã¯ã©ã¹ãã¡ã¤ã«ã®ã¿ã¼ã²ããã "Unit Test"ã«ãã¦ãã。ãããããªãã¨ãã¹ãã±ã¼ã¹ãå®è¡ãããªã。
ãã¹ãã±ã¼ã¹ããµã³ãã«ã¨ãã¦èªåçæããããããã¯åé¤ãã¦è¯ã4. ãã¹ãã±ã¼ã¹å®è¡
ã¾ãæåã«ãããªãã¹ãã±ã¼ã¹ãæ¸ãã¦ã¿ã。
- (void)testGreeting { counter_++; SampleClass* obj = [[[SampleClass alloc] init] autorelease]; STAssertTrue([[obj greeting] isEqualToString:@"x"], @"failed"); }ãã¹ãç¨ã¡ã½ããåã¯testã§éå§ããå¿ è¦ããã。
ã¢ã¯ãã£ãã¿ã¼ã²ããã« "Unit Test" ãé¸æãã¦ãã«ããã。
åºã。
ããã£ããã¨ãªã©
ãã¹ãã±ã¼ã¹å®è¡ãæåããã®ã§ããããããã£ã¦ã¿ã。ããã£ããã¨ã¯æ¬¡ã®éã。
STAssert* ãã¯ãã¯å¯å¤å¼æ°ãåã
STAssert*ãã¯ãã¯å¯å¤å¼æ°ã§ NSLog() ã®æ§ã«è¿½å æ å ±ã表示ãããã¨ãã§ãã。ãããªæã。STAssertTrue([[obj greeting] isEqualToString:@"x"], @"*%d-%@*", 1, @"A");
ãªãã¡ãã»ã¼ã¸ã« ":" ãå«ããã¨ããããåã®æåã表示ãããªã。
(ä¾)@"failed: A" => A ã¨è¡¨ç¤ºããã
ã¾ã、STAssertTrue(..., @"%@", self) ã¨ããã¨å®è¡ä¸ã®ã¡ã½ããåã表示ããã。
ã¡ã³ãå¤æ°
ã¡ã³ãå¤æ°ã使ããã¨ãå¯è½。@interface SampleClassTest : SenTestCase { NSInteger counter_; }
ãã¹ãã±ã¼ã¹å®è¡é ã¯ã¡ã½ããåæé
ãã¹ãã¡ã½ããã追å ãã¦ã¿ã。å®è¡é åºã¯ã¡ã½ããåæé ã®ããã 。- (void)testFirst { counter_++; STAssertTrue(NO, @"counter=%d", counter_); }
ãã¹ãã±ã¼ã¹æ¯ã«ã¤ã³ã¹ã¿ã³ã¹ãçæããã
ããããã®ã¡ã½ããã§ã¢ãã¬ã¹ã表示ããã¦ã¿ãã¨å¥ã®å¤ã表示ããã。ãã¹ãã¡ã½ããå®è¡æ¯ã«ã¤ã³ã¹ã¿ã³ã¹ã¯æ°è¦ä½æããã¦ããã®ãããã。è¤æ°ã®ãã¹ãã±ã¼ã¹ã¯ã©ã¹
ãããååé ã«å®è¡ããã。ãããã°è¡¨ç¤ºã§ããªã
NSLog(), fputs("..", stderr) ãªã©ã§ãã¹ãã±ã¼ã¹ããã®åºåãè¦ããã¨ãã§ããªã。åæåã¨å¾å§æ«
setUp/tearDown ã¡ã½ãããæ¸ãã¨ãã¹ãã±ã¼ã¹å®è¡åå¾ã«å¦çãå®è¡ããããã¨ãã§ãã。-(void) setUp { // åæåå¦ç counter_ = 100; : }
ãã®ä»
å®æ©ãã«ãã§ã¯ãã¹ãã§ããªã。ãã¹ãã¡ã½ããã¯ããããã¡ã¤ã«ã§å®£è¨ããå¿ è¦ã¯ç¡ã
åè:ãã¹ããã¯ãã®ç¨®é¡
SenTestCase.h ãã#define STAssertNil(a1, description, ...) #define STAssertNotNil(a1, description, ...) #define STAssertTrue(expression, description, ...) #define STAssertFalse(expression, description, ...) #define STAssertEqualObjects(a1, a2, description, ...) #define STAssertEquals(a1, a2, description, ...) #define STAssertEqualsWithAccuracy(left, right, accuracy, description, ...) #define STAssertThrows(expression, description, ...) #define STAssertThrowsSpecific(expression, specificException, description, ...) #define STAssertThrowsSpecificNamed(expr, specificException, aName, description, ...) #define STAssertNoThrow(expression, description, ...) #define STAssertNoThrowSpecific(expression, specificException, description, ...) #define STAssertNoThrowSpecificNamed(expr, specificException, aName, description, ...) #define STFail(description, ...) #define STAssertTrueNoThrow(expression, description, ...) #define STAssertFalseNoThrow(expression, description, ...)
åèæ å ±
OCUnit ã«ã¤ãã¦ã¯ [SM gakusyuu]; ãµã¤ãã詳ãã。Cocoaã¢ããªãã Static Library、ãã¹ãã±ã¼ã¹ã®ãããã°、æã¦ã¯ã³ã¼ãã«ãã¬ãã¸è¨æ¸¬ã¾ã§ç¶²ç¾ ããã¦ãã¦é常ã«åèã«ãªã£ã。
OCUnitã®ä½¿ãæ¹(Cocoa Applicationç·¨) - [SM gakusyuu];
OCUnitã®ä½¿ãæ¹(Static Libraryç·¨) - [SM gakusyuu];
OCUnitã®ä½¿ãæ¹(ãããã°ç·¨) - [SM gakusyuu];
OCUnitã®ä½¿ãæ¹(ã³ã¼ãã«ãã¬ãã¸è¨æ¸¬) - [SM gakusyuu];
å ¬å¼ãªãã¡ã¬ã³ã¹(iOSåã)
iOS Development Guide: Unit Testing Applications
ã½ã¼ã¹ã³ã¼ã
GitHub ããã©ãã。
StudyUnitTest at 2010-11-22 from xcatsan's iOS-Sample-Code - GitHub
Responses
Leave a Response