1

I'm writing Cocos2d application and looking for a way to write functional tests.

What I really need, is to launch application on the simulator and check that my scene contains specific nodes. Something like this

@implementation MenuTest

- (void) setUp
{
    // Launch app on the simulator
}

- (void) tearDown
{
    // Shut simulator down
}

- (void) testMenuContainsExitItem
{
    CCScene *scene = [[CCDirector sharedDirector] runningScene];
    CCNode *exit = [scene getChildByTag:EXIT_ITEM];

    STAssertNotNil(exit, @"No exit item found");
}

@end

So, is there a way to execute tests on a running app?

lambdas
  • 3,990
  • 2
  • 29
  • 54

2 Answers2

0

You create and setup the scene in the setup method, then use the test* methods to check if the scene was set up according to specifications.

Be sure to use a function in setup that you're also calling from your game code. Something like:

-(void) setup
{
    CCScene* scene = [FirstScene setup];
    [[CCDirector sharedDirector] runWithScene:scene];
}

Then perform the tests:

- (void) testMenuContainsExitItem
{
    CCScene* scene = [CCDirector sharedDirector].runningScene;
    CCNode *exit = [scene getChildByTag:EXIT_ITEM];

    STAssertNotNil(exit, @"No exit item found");
}

You do not need to nor should you add any of the OCUnit test code (ie STAssert*) in your scene's code. Because then your scene wouldn't compile in regular builds without OCUnit.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
-1

You can define different tags for all CCNodes in your app. For example in interface section add something like it:

#define kExitNodeTag = 400;

While initialization:

-(id)init {
    if(self=[super init]) {
         ...
         self.tag = kExitNoteTag;
         ...
    }
}

You can check for node existence after initialization in every CCScene:

-(void)onEnter {
    CCNode *exit = [scene getChildByTag:kExitNodeTag];
    STAssertNotNil(exit, @"No exit item found");
}
anatoliy_v
  • 1,782
  • 15
  • 24
  • I know it is possible to set tags on nodes. But how do I run my OCUnit tests from within `onEnter` method? – lambdas Oct 25 '12 at 11:31