I write unit test case with XCTestCase, and initialize variables in -setUp as following:
- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_myPath = @"Path";
});
}
But when I try to use myPath in test cases, it only works in the first one, and the "myPath" would be in nil in subsequent cases.
So I set break point in -setUp to see what's happen. And I found that it create new instances for each methods!!
To double check it, I create a new project and test target to log testcase address as following:
@implementation fooTestTests
- (void)setUp
{
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
NSLog(@"<fooTestTests: %p>", self);
}
- (void)tearDown
{
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
- (void)testExample
{
XCTFail(@"No implementation for \"%s\"", __PRETTY_FUNCTION__);
}
- (void)testExample2
{
XCTFail(@"No implementation for \"%s\"", __PRETTY_FUNCTION__);
}
@end
And the result is :
fooTest[846:303] <fooTestTests: 0x1005ab750>
fooTest[846:303] <fooTestTests: 0x1005ab7f0>
Since the XCTestCase was designed as a object have one or more test method, it should not create different instance for each method.
In such situation, I don't know where to setup my test environment. Even though write setup code in -init, it still create new instance and call -init many times. Currently, I have only a few unit test, but when the number of tests grows, and the setup procedure becomes more complex, it would be a problem. Could anyone give me a suggestion?
Add Question Summary:
If I have 2 test methods in one testcase class, the behaviour would be:
- Instantiate new testcase as object 1
- -setUp
- test 1
- -tearDown
- Instanitate new testcase as object 2
- -setUp
- test 2
- -tearDown
Why it needs step 5?
The Answer
The answer provided by Jon Reid
More information: