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
方法会在每个测试方法运行前调用,可以把一些通用的设置放在这个方法中进行 -
tearDown
方法会在所有测试方法结束后调用,可以用来做一些置空或者记录等操作 -
testExample
是一个测试方法示例,方法名必须以test开头,返回值为void -
testPerformaceExample
方法是一个测试时间性能的方法示例,里面调用的measureBlock
方法是XCTestCase
类定义的方法,会自动测算block中代码的执行时间
常用的XCTest断言
- 空测试
-
XCTAssert(expression,format...)
说明:基础测试,当expression不满足时,会执行format...的内容,一般为一个说明的字符串
示例:XCTAssert(response, @"response is nil")
-
XCTAssertNil(expression, format...)
说明:当expression不为空时,会报错,内容为format... XCTAssertNotNil(expression, format...)
-
- 无条件报错
XCFail(format...)
说明:无论是否有误,都会报错,内容为format... - 等价测试
-
XCTAssertEqaulObjects(object1, object2, format...)
说明:判断两个对象的内容是否相等,不相等则报错
ps. 经测试,字符串会判断字符串是否一样,相当于isEqualToString
-
XCTAssertNotEqaulObjects(object1, object2, format...)
说明:与上面相反 -
XCTAssertEqual(object1, object2, format...)
说明:判断两个对象是否为同一个
ps. 字符串相当于isEqualTo
-
XCTAssertNotEqual(object1, object2, format...)
说明:与上面个相反 -
XCTAssertEqualWithAccuracy(var1, var2, accuracy, format...)
说明:允许var1和var2之间最多相差accuracy,一般用于比较NSInteger等数值类型 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, ...)
说明:当expression不抛出异常时报错 -
XCTAssertThrowsSpecific(expression, exception_class, ...)
说明:当expression不抛出指定类型的异常时报错 -
XCTAssertThrowsSpecificNamed(expression, exception_class, exception_name, ...)
说明:当expression不抛出指定类型的指定异常时报错 XCTAssertNotThrows(expression, ...)
XCTAssertNotThrowsSpecific(expression, exception_class, ...)
XCTAssertNotThrowsSpecificNamed(expression, exception_class, exception_name, ...)
-
异步测试
XCode6之后提供了一个XCTestExpectation
类
用法如下:
//创建一个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");
}
//若所有的操作都执行完毕,则发送fulfill消息
[expectation fulfill];
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
XCTFail(@"request failed");
[expectation fulfill];
}];
//等待一定时长,若在该时间段内没有收到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
- 应用CocoaPods管理的项目如何接入单元测试
http://www.jianshu.com/p/8211b873a401 - http://stackoverflow.com/questions/26031395/assertion-failure-in-afhttprequestserializer-requestwithmethodurlstringparam