NSUnit 是 Objective-C 项目的单元测试框架。NSUnit 基于 SenTestingKit,提供了更加符合 Objective-C 的 API,特别是针对 Foundation 类的测试。
断言类是 NSUnit 的主要测试入口点。测试通常形式如下:
[Assert that:<subject> <verb>:<condition>]
一些示例
[Assert that:name isNot:[AnEmpty string]];
[Assert that:name isNot:[Equal to:@"Omar"]];
[Assert that:name is:[Equal to:"McNulty"]];
[Assert that:someArrayOfInts areAll:[Less than:@20]];
使用了 The article,可以将诸如 double 和 int 这样的原始数据类型自动转换为其相应的 NSObject 类型。
一些示例
[Assert that:[The int:i] is:[Equal to:@25]];
[Assert that:[The double:d] is:[Less than:@10]];
NSUnit 的 Assert 类提供了一个确保测试失败的方法。`fails:(void (^)(void))` 方法确保提供的块抛出异常。例如:
[Assert fails:^{
[WebService connectToUrl:nil];
}];
以下是一些可用的动词
is - The condition evaluates to true.
isNot - The condition evaluates to false.
areAll - Each value in the provided collection evaluates to true.
areAllNot - Each value in the provided collection evaluates to false.
以下是一些可用的条件
Equal to: - The subject equals the provided value.
TheSame as: - The subject is the exact same object as the provided value.
Less than: - The subject is less than the provided value. Both values must be non-nil numbers.
Less thanOrEqual: - The subject is less than or equal to the provided value. Both values must be non-nil numbers.
Greater than: - The subject is greater than the provided value. Both values must be non-nil numbers.
Greater thanOrEqual: - The subject is greater than or equal to the provided value. Both values must be non-nil numbers.
Between low: and: - The subject is a number between the two provided values, inclusive. All values must be non-nil numbers.
AnEmpty array: - The subject is an empty array.
AnEmpty string: - The subject is an empty string.
更多示例可以在 NSUnit 的 单元测试 中找到。
NSUnit 使用 iOS Universal Framework 构建。