iossentestingkit

Variable destroyed in unit test


all:

I've the following unit tests class (implementation file shown):

@implementation SampleTests {
    A* c;
    NSString* token;
}

- (void)setUp
{
    [super setUp];

    // Set-up code here.
    c = [[A alloc] init];
}

- (void)tearDown
{
    // Tear-down code here.

    [super tearDown];
}

- (void)testA
{
    token = @"sample";
}

-(void)testB
{
    [c method:token]; // <-- fails because token is nil, but c has a correct value. Why!
}

@end

When I run my tests, testB fails because token is nil, but c it's ok, so why is it token destroyed?


Solution

  • Every time you run your unit tests, each test case is invoked independently. Before each test case runs, the setUp method is called, and afterwards the tearDown method is called.

    So if you want to share token among tests, you should add

    token = @"sample"; // or smth else 
    

    to your setUp method.