【问题标题】:Link two or more SenTestCase链接两个或多个 SenTestCase
【发布时间】:2014-02-10 15:44:55
【问题描述】:

我正在尝试更改现有的 defaultTestSuite 方法以创建一个类,该类可以从不同的类中选择测试方法并按特定顺序执行它们。 但是,每次我在另一个测试文件中导入测试文件时,我都会收到链接器错误

duplicate symbol _OBJC_METACLASS_$_TMKTestExample

为什么 OCUnit 会出现这种情况,我该如何解决?

问候

【问题讨论】:

  • 单元测试应该独立,不依赖于测试运行顺序。你有必要这样做吗?
  • 我正在使用 KIF,我希望重用自定义测试类来执行 UI 测试流程,而不是一遍又一遍地编写测试代码。

标签: ios objective-c ocunit


【解决方案1】:

检查您的TMKTestExample 的Target Membership,它不应同时包含在主要目标和单元测试目标中。

【讨论】:

    【解决方案2】:

    我通过在属于 SenTestCase 的 testInvocation 方法中创建自己的 NSInvocations 找到了解决方案。

    基本上我有一个空的测试类,我抛出执行某些操作(用于流测试)的测试方法,这些方法在每个 NSInvocation 中设置,一旦测试运行,这些方法将以与存在的顺序相同的顺序执行testInvocation 静态方法,它允许添加尽可能多的方法,类似于下面的粗略示例:

    扩展 SenTestCase 类

    - (id) initWithInvocation:(NSInvocation *)anInvocation
    {
        id invocationTarget = anInvocation.target;
        self = [super initWithInvocation:anInvocation];
    
        if (!self) {
            return nil;
        }
    
        if (invocationTarget != nil && invocationTarget != self) {
            anInvocation.target = invocationTarget;
        }
    
        return self;
    
    }
    

    需要重写上面的方法,因为在super initWithInvocation方法中target总是会设置为self。

    +(NSArray *) testInvocations
    {
    
    
        NSMutableArray *invocations = (NSMutableArray *)[super testInvocations];
        TMKTestFirstViewControllerBaseAction *baseAction = [TMKTestFirstViewControllerBaseAction sharedInstance];
        for (int i=0; i<4; i++) {
            NSInvocation *invocation = nil;
    
            switch (i) {
                case 3:{
                    invocation = [NSInvocation invocationWithTarget:baseAction      selector:@selector(tapChangeBackgroundButton)];
                    break;
                }
                case 2:{
                    invocation = [NSInvocation invocationWithTarget:baseAction selector:@selector(tapChangeBackgroundButton)];
                    break;
                }
                case 0:{
                    invocation = [NSInvocation invocationWithTarget:baseAction selector:@selector(tapBarButtonWithAccessibilityLabel:)];
                    NSString *arg = @"Second";
                [invocation setArgument:&arg atIndex:2];
                    break;
                }
                case 1:{
                invocation = [NSInvocation invocationWithTarget:baseAction  selector:@selector(tapBarButtonWithAccessibilityLabel:)];
                    NSString *arg = @"First";
                    [invocation setArgument:&arg atIndex:2];
                    break;
                }
                default:
                    break;
            }
            [invocation retainArguments];
            NSLog(@"invocation target: %d target: %@", i,invocation.target);
             [invocations insertObject:invocation atIndex:i+1];
    
    
        }
    
        return invocations;
    }
    

    上面的例子只增加了一个测试,但可以看出我的意思,这是取自这些网站的例子:

    使用这两种方法并控制测试顺序,我可以为 KIF 创建以下内容: - 描述在特定 UIViewController/UIView/etc. 中测试的动作的类,然后重用这些方法来创建流测试、回归 UI 测试、从代码或脚本(仍在执行后者)。 - 正常的测试课程。

    我希望这对需要这个非常具体要求的人有所帮助。

    关于我还没有弄清楚的原始问题,我想知道是否有办法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-05
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      • 2012-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多