OGeek|极客世界-中国程序员成长平台

标题: ios - XCTestCase for ios 的 XCTestSuite [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:11
标题: ios - XCTestCase for ios 的 XCTestSuite

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

我将能够运行相同的 XCTestCase 派生类并注入(inject)测试上下文。我以为我会为此使用 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];

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



Best Answer-推荐答案


通过查看代码,我能够实现我想做的事情 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 类中实现它...

关于ios - XCTestCase for ios 的 XCTestSuite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32860212/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4