我在我的iOS应用程序中使用KIF进行集成/验收测试,我有一个示例需要运行~50个静态表行,期望将视图上的特定内容压入堆栈。
如果我在Cucumber / Rspec世界,我会写一个类似于Cucumber的例子的场景大纲:
Scenario Outline: eating
Given there are <start> cucumbers
When I eat <eat> cucumbers
Then I should have <left> cucumbers
Examples:
| start | eat | left |
| 12 | 5 | 7 |
| 20 | 5 | 15 |
每个示例都会运行方案并记录单个通过/失败。有没有一种简单的方法可以用KIF(2.0)重新创建它?我几乎可以通过循环遍历每个“示例”来重新创建它,并在循环执行失败时报告失败,但我担心这只会在实际上测试多个示例时出现失败。
答案 0 :(得分:0)
我在提出问题后意识到我真正需要用来实现这个目标的是SenTestingKit(不一定是KIF)。我使用http://briancoyner.github.io/blog/2011/09/12/ocunit-parameterized-test-case/解决方案的变体来使用相同的测试方法运行多个测试,每个测试运行不同的值。
我在SenTestCase子类中添加了description
方法,该方法为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
这里有足够的空间可供改进,但这是一个很好的起点。