Question

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?

Was it helpful?

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.

OTHER TIPS

As far as I know, the current implementation runs the test methods in their alphabetic order, so your example should run without problems.

Usually, if I want something to be tested first, I name the methods test1_criticalFeature, test2_dependentFeatures etc.

The order that test methods are executed in is not guaranteed, so it could be the case that testB is run before testA or even in the future that they are run in parallel.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top