【问题标题】:XCTestSuite of XCTestCase for iosXCTestCase for ios 的 XCTestSuite
【发布时间】:2015-12-27 21:24:49
【问题描述】:

我需要测试一个UIViewController,它的行为取决于给它的参数(控件是基于网络服务调用在viewDidLoad 中动态实例化的)。

我将能够运行相同的XCTestCase 派生类并注入测试上下文。我以为我会为此使用XCTestSuite,但事实并非如此,因为XCTestSuite 是一组XCTest 而不是XCTestCase

基本上我想做:

XCTestCaseSuite* suite = [[XCTestCaseSuite alloc] init];
for (Condition* condition in conditions) {
  MyTestCase* case = [[MyTestCase alloc] initWithCondition:condition];
  [suite addTestCase:case];
}
[suite runTest];

有没有办法做到这一点? 谢谢!

【问题讨论】:

    标签: ios xcode unit-testing testing xctest


    【解决方案1】:

    通过查看代码,我能够实现我想做的事情 https://github.com/michalkonturek/XCParameterizedTestCase

    具体来说,我复制了https://github.com/michalkonturek/XCParameterizedTestCase/blob/master/Source/XCParameterizedTestCase.m 中的机制,并且能够做我想做的事。

    此方法的一个缺点是同一测试的所有实例都以相同的方式报告,例如,无法知道哪个特定实例失败了。为了避免这种情况,我添加了继承自 XCTestCase 基类的动态类创建:

    // create a dynamic class so that reporting is good
    NSString* testClassName = [NSString stringWithFormat:@"%@Test", [condition.description capitalizedString]];
    Class testClass = objc_allocateClassPair([MyTestCase class], [testClassName UTF8String], 0);
    objc_registerClassPair(testClass);
    

    然后你可以实例化每个测试用例类

    XCTestCase *test = [[NSClassFromString(testClassName) alloc] initWithInvocation:invocation
                                                                        forCondition:condition];
    

    我将尝试在一个非常通用的 XCParameterizedTestCase 类中实现它...

    【讨论】:

      猜你喜欢
      • 2014-02-13
      • 1970-01-01
      • 2017-05-26
      • 1970-01-01
      • 2016-02-05
      • 2014-01-27
      • 1970-01-01
      • 1970-01-01
      • 2016-10-05
      相关资源
      最近更新 更多