【问题标题】:Functional testing cocos2d applicationcocos2d应用功能测试
【发布时间】:2012-10-25 10:43:11
【问题描述】:

我正在编写 Cocos2d 应用程序并寻找编写功能测试的方法。

我真正需要的是在模拟器上启动应用程序并检查我的场景是否包含特定节点。像这样的

@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

那么,有没有办法在正在运行的应用程序上执行测试?

【问题讨论】:

    标签: testing cocos2d-iphone functional-testing ocunit


    【解决方案1】:

    在 setup 方法中创建并设置场景,然后使用 test* 方法检查场景是否按照规范设置。

    请务必在设置中使用您也从游戏代码中调用的函数。比如:

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

    然后进行测试:

    - (void) testMenuContainsExitItem
    {
        CCScene* scene = [CCDirector sharedDirector].runningScene;
        CCNode *exit = [scene getChildByTag:EXIT_ITEM];
    
        STAssertNotNil(exit, @"No exit item found");
    }
    

    您不需要也不应该在场景代码中添加任何 OCUnit 测试代码(即 STAssert*)。因为如果没有 OCUnit,您的场景将无法在常规构建中编译。

    【讨论】:

    • 所以不需要在模拟器上启动我的应用来测试场景?
    • 当我的场景实例化 CClabelTTF 时出现可悲的异常。
    【解决方案2】:

    您可以为应用中的所有 CCNode 定义不同的标签。例如在界面部分添加类似的内容:

    #define kExitNodeTag = 400;
    

    初始化时:

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

    您可以在每个 CCScene 初始化后检查节点是否存在:

    -(void)onEnter {
        CCNode *exit = [scene getChildByTag:kExitNodeTag];
        STAssertNotNil(exit, @"No exit item found");
    }
    

    【讨论】:

    • 我知道可以在节点上设置标签。但是如何在 onEnter 方法中运行我的 OCUnit 测试?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 2023-03-30
    • 2017-10-05
    相关资源
    最近更新 更多