0

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) で簡単に再現する方法はありますか? 各「例」をループして、ループの 1 つの実行が失敗した場合に失敗を報告することで、ほぼ再現できましたが、実際には複数の例がテストされたときに 1 つの失敗としてのみ表示されるのではないかと懸念しています。

4

1 に答える 1

0

これを達成するために実際に使用する必要があるのは SenTestingKit (必ずしも KIF である必要はありません) であるという質問をした後、私は気付きました。http://briancoyner.github.io/blog/2011/09/12/ocunit-parameterized-test-case/のソリューションのバリエーションを使用して、同じテスト方法と実行ごとに異なる値を使用して複数のテストを実行しました。

descriptionXcode 5 のテスト ナビゲーターをサポートするメソッドを SenTestCase サブクラスに追加しました。1 つのテスト メソッドで、各パラメーター セットのテストの実行を確認できます。メソッドを変更することで、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

異なるパラメーターを使用した同じテスト メソッドの一意の実行

ここには改善の余地がたくさんありますが、ここから始めるのが良いでしょう。

于 2013-11-05T19:04:21.603 に答える