UnitTest Class
#import <XCTest/XCTest.h>
@interface UnitConversionsTests : XCTestCase
@end
@implementation UnitConversionsTests
- (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.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
- (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
說明:
-
setup
方法會在每個測試方法運(yùn)行前調(diào)用,可以把一些通用的設(shè)置放在這個方法中進(jìn)行 -
tearDown
方法會在所有測試方法結(jié)束后調(diào)用,可以用來做一些置空或者記錄等操作 -
testExample
是一個測試方法示例药磺,方法名必須以test開頭汰具,返回值為void -
testPerformaceExample
方法是一個測試時間性能的方法示例泄私,里面調(diào)用的measureBlock
方法是XCTestCase
類定義的方法重荠,會自動測算block中代碼的執(zhí)行時間
常用的XCTest斷言
- 空測試
-
XCTAssert(expression,format...)
說明:基礎(chǔ)測試,當(dāng)expression不滿足時惭缰,會執(zhí)行format...的內(nèi)容浪南,一般為一個說明的字符串
示例:XCTAssert(response, @"response is nil")
-
XCTAssertNil(expression, format...)
說明:當(dāng)expression不為空時,會報錯漱受,內(nèi)容為format... XCTAssertNotNil(expression, format...)
-
- 無條件報錯
XCFail(format...)
說明:無論是否有誤,都會報錯骡送,內(nèi)容為format... - 等價測試
-
XCTAssertEqaulObjects(object1, object2, format...)
說明:判斷兩個對象的內(nèi)容是否相等昂羡,不相等則報錯
ps. 經(jīng)測試,字符串會判斷字符串是否一樣摔踱,相當(dāng)于isEqualToString
-
XCTAssertNotEqaulObjects(object1, object2, format...)
說明:與上面相反 -
XCTAssertEqual(object1, object2, format...)
說明:判斷兩個對象是否為同一個
ps. 字符串相當(dāng)于isEqualTo
-
XCTAssertNotEqual(object1, object2, format...)
說明:與上面?zhèn)€相反 -
XCTAssertEqualWithAccuracy(var1, var2, accuracy, format...)
說明:允許var1和var2之間最多相差accuracy虐先,一般用于比較NSInteger等數(shù)值類型 XCTAssertNotEqualWithAccuracy(var1, var2, accuracy, format...)
XCTAssertGreaterThan(expression1, expression2, ...)
XCTAssertGreaterThanOrEqual(expression1, expression2, ...)
XCTAssertLessThan(expression1, expression2, ...)
XCTAssertLessThanOrEqual(expression1, expression2, ...)
-
- BOOL測試
XCTAssertTrue(expression, format...)
XCTAssertFalse(expression, format...)
- 異常測試
-
XCTAssertThrows(expression, ...)
說明:當(dāng)expression不拋出異常時報錯 -
XCTAssertThrowsSpecific(expression, exception_class, ...)
說明:當(dāng)expression不拋出指定類型的異常時報錯 -
XCTAssertThrowsSpecificNamed(expression, exception_class, exception_name, ...)
說明:當(dāng)expression不拋出指定類型的指定異常時報錯 XCTAssertNotThrows(expression, ...)
XCTAssertNotThrowsSpecific(expression, exception_class, ...)
XCTAssertNotThrowsSpecificNamed(expression, exception_class, exception_name, ...)
-
異步測試
XCode6之后提供了一個XCTestExpectation
類
用法如下:
//創(chuàng)建一個XCTestExpectation類,通過描述來初始化
XCTestExpectation *expectation = [self expectationWithDescription:@"AFNetworking asynchronousTest"];
AFHTTPSessionManager *httpSessionManager = [AFHTTPSessionManager manager];
[httpSessionManager GET:_testUrl
parameters:nil
progress:^(NSProgress * _Nonnull downloadProgress) {
}
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
XCTAssertNotNil(responseObject, @"responseObject is nil");
if (![responseObject isKindOfClass:[NSDictionary class]]) {
XCTFail(@"responseObject is not a dictionary");
}
//若所有的操作都執(zhí)行完畢派敷,則發(fā)送fulfill消息
[expectation fulfill];
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
XCTFail(@"request failed");
[expectation fulfill];
}];
//等待一定時長蛹批,若在該時間段內(nèi)沒有收到fulfill消息則會報錯
[self waitForExpectationsWithTimeout:10.0 handler:^(NSError * _Nullable error) {
if (error) {
XCTFail(@"time out with error %@", error);
}
}];
遇到的問題
- 與CocoaPods一起使用時,用CocoaPods管理的第三方庫無法在test類中引用
解決方法:
在Podfile的第一行添加:
link_with['AsynchronousTestDemo','AsynchronousTestDemoTests','AsynchronousTestDemoUITests']
參考鏈接
- Testing with Xcode文檔(中文版)
http://www.cocoachina.com/ios/20140715/9144.html - XCTestAssertions.h
- 應(yīng)用CocoaPods管理的項(xiàng)目如何接入單元測試
http://www.reibang.com/p/8211b873a401 - http://stackoverflow.com/questions/26031395/assertion-failure-in-afhttprequestserializer-requestwithmethodurlstringparam