且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Xcode 4中运行逻辑测试而无需启动模拟器

更新时间:2023-02-20 20:30:40

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

我使用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.