在Xcode 4中运行逻辑测试而无需启动模拟器 [英] Run logic tests in Xcode 4 without launching the simulator

查看:138
本文介绍了在Xcode 4中运行逻辑测试而无需启动模拟器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在不启动模拟器的情况下使用OCUnit在Xcode 4中运行测试.请不要试图说服我我正在做错误的单元测试或类似的事情.我喜欢用传统的方式进行TDD:在测试中为类编写API,然后使类通过测试.我将编写在模拟器中运行的端到端的单独测试.

如果无法执行此操作,那么有人可以告诉我如何让测试工具不实例化整个应用程序吗?我的应用程序是事件驱动的,它在启动时会发送大量事件,这些事件会干扰我的测试.

解决方案

请有人告诉我如何让测试工具不实例化整个应用程序吗?我的应用程序是事件驱动的,它在启动时会发送大量事件,这些事件会干扰我的测试.

我使用Xcode 4的内置测试.应用程序实例化似乎有些痛苦,但是正如我在 Xcode单元测试中所写的那样: Ugly ,它使得编写测试而无需区分逻辑测试和应用程序测试成为可能.具体来说,它使我可以为视图控制器编写单元测试.

这是我要避免完全启动的步骤:

编辑方案

  • 选择测试"操作
  • 在测试"中,选择参数"标签
  • 禁用使用运行"操作的选项"
  • 添加一个环境变量,将runningTests设置为YES

编辑您的应用程序委托

  • 将以下内容尽快添加到-application:didFinishLaunchingWithOptions::

    #if DEBUG
        if (getenv("runningTests"))
            return YES;
    #endif
    

  • -applicationDidBecomeActive:执行相同的操作,但只需return.

更新:我已经更改了方法.请参阅如何轻松切换应用程序代表进行测试.

I want to run tests in Xcode 4 using OCUnit without launching the simulator. Please, don't try and convince me I am doing unit testing wrong or anything like that. I like to do TDD the traditional way: write the API for the class in the tests, then make the class pass the tests. I will write separate tests that are end-to-end that run in the simulator.

If there's no way to do this, then please can someone tell me how to have the test harness not instantiate the whole app? My app is event driven, and it sends a bunch of events through when it starts up that mess with my tests.

解决方案

Please can someone tell me how to have the test harness not instantiate the whole app? My app is event driven, and it sends a bunch of events through when it starts up that mess with my tests.

I use Xcode 4's built-in testing. App instantiation may seem like a pain, but as I write on Xcode Unit Testing: The Good, the Bad, the Ugly, it makes it possible to write tests without distinguishing between logic tests and application tests. Specifically, it lets me write unit tests for view controllers.

Here's what I do to avoid my full startup sequence:

Edit the scheme

  • Select the Test action
  • In "Test" select the Arguments tab
  • Disable "Use the Run action's options"
  • Add an environment variable, setting runningTests to YES

Edit your app delegate

  • Add the following to -application:didFinishLaunchingWithOptions: as soon as it makes sense to:

    #if DEBUG
        if (getenv("runningTests"))
            return YES;
    #endif
    

  • Do the same for -applicationDidBecomeActive: but simply return.

Update: I have changed my approach. See How to Easily Switch Your App Delegate for Testing.

这篇关于在Xcode 4中运行逻辑测试而无需启动模拟器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆