在问了这个问题后,我意识到我真正需要使用的是 SenTestingKit(不一定是 KIF)。我使用了来自http://briancoyner.github.io/blog/2011/09/12/ocunit-parameterized-test-case/的解决方案的变体,以使用相同的测试方法和每次运行的不同值运行多个测试。
description
我在我的 SenTestCase 子类中添加了一个方法,它为 Xcode 5 的测试导航器提供支持。使用一种测试方法,您可以查看每个参数集的测试运行情况。通过修改description
方法,您可以唯一地命名每次运行。该方法类似于我链接到的博客文章,但我将在此处提供我的实现:
WMScenarioOutline.h
#import <KIF/KIF.h>
@interface WMScenarioOutline : SenTestCase
@property (nonatomic, copy) NSDictionary *example;
- (id)initWithInvocation:(NSInvocation *)anInvocation example:(NSDictionary *)example;
@end
WMScenarioOutline.m
#import "WMScenarioOutline.h"
@implementation WMScenarioOutline
+ (id)defaultTestSuite
{
SenTestSuite *testSuite = [[SenTestSuite alloc] initWithName:NSStringFromClass(self)];
[self addTestCaseWithExample:@{@"name" : @"Foo", @"value" : @0} toTestSuite:testSuite];
[self addTestCaseWithExample:@{@"name" : @"Bar", @"value" : @1} toTestSuite:testSuite];
return testSuite;
}
+ (void)addTestCaseWithExample:(NSDictionary *)example toTestSuite:(SenTestSuite *)suite
{
NSArray *testInvocations = [self testInvocations];
for (NSInvocation *testInvocation in testInvocations) {
SenTestCase *testCase = [[WMScenarioOutline alloc] initWithInvocation:testInvocation example:example];
[suite addTest:testCase];
}
}
- (id)initWithInvocation:(NSInvocation *)anInvocation example:(NSDictionary *)example
{
self = [super initWithInvocation:anInvocation];
if (self) {
_example = example;
}
return self;
}
- (void)testScenarioOutline
{
[tester runBlock:^KIFTestStepResult(NSError *__autoreleasing *error) {
NSLog(@"Testing example: %@", self.example);
return [self.example[@"value"] intValue] == 0 ? KIFTestStepResultSuccess : KIFTestStepResultFailure;
}];
}
- (NSString *)description
{
NSInvocation *invocation = [self invocation];
NSString *name = NSStringFromSelector(invocation.selector);
if ([name hasPrefix:@"test"]) {
return [NSString stringWithFormat:@"-[%@ %@%@]", NSStringFromClass([self class]), NSStringFromSelector(invocation.selector), self.example[@"name"]];
}
else {
return [super description];
}
}
@end
这里有很大的改进空间,但这是一个很好的起点。