【问题标题】:Possible to build a scenario outline with multiple examples with KIF?可以使用 KIF 构建包含多个示例的场景大纲吗?
【发布时间】:2013-11-15 15:12:07
【问题描述】:

我在我的 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) 重新创建它?我几乎可以通过循环遍历每个“示例”并在循环的一次执行失败时报告失败来重新创建它,但我担心在实际测试多个示例时只会显示为一次失败。

【问题讨论】:

    标签: ios objective-c integration-testing kif-framework kif


    【解决方案1】:

    在问了这个问题后,我意识到我真正需要使用的是 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
    

    这里有很大的改进空间,但这是一个很好的起点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-19
      • 2014-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多