【发布时间】:2014-01-29 01:48:40
【问题描述】:
根据XCTestCase 默认模板中关于setUp 的评论:
Put setup code here; it will be run once, before the first test case.
但是,在XCTestCase.h 中,setUp 上方的评论表述不同:
Setup method called before the invocation of each test method in the class.
为了确认实际行为,我在setUp 中放了一个NSLog 来计算它被调用了多少次:
static int count = 0;
- (void)setUp
{
[super setUp];
count++;
NSLog(@"Call Count = %d", count);
}
这导致在每个测试方法之前调用setUp 方法(确认XCTestCase.h 上的评论)。
我想使用setUp 方法一次创建测试/模拟对象(例如设置Core Data 测试堆栈)。一遍又一遍地创建这些将是处理器密集型的,并且可能非常慢。
所以,
1) setUp 的实际用途是什么?开发人员肯定不会一遍又一遍地在其中创建对象吗?
2) 我怎样才能在XCTestCase 中仅一次创建这些对象?
【问题讨论】: