我需要测试一个UIViewController,它的行为依赖于提供给它的参数(控件是基于are服务调用在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];有没有办法做到这一点?谢谢!
发布于 2015-10-01 03:11:16
通过查看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类中实现这一点...
https://stackoverflow.com/questions/32860212
复制相似问题