如何运行和调试iPhone应用程序的单元测试 [英] How to run and debug unit tests for an iPhone application

查看:88
本文介绍了如何运行和调试iPhone应用程序的单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:现在单元测试更容易设置。本教程与Xcode版本5及更高版本并不相关。

我花了很长时间但是我终于设法让它适用于我的项目。
为了创建逻辑测试我跟着 Apple关于创建逻辑测试的指南
一旦你理解逻辑测试在构建期间运行,这样就可以了。

It took me quite some time but I finally managed to make it work for my project. To create the "logic" tests I followed Apple guidelines on creating logic tests. This works fine once you understand that the logic tests are run during build.

为了能够调试这些测试,需要创建一个自定义可执行文件将调用这些测试。 Sean Miceli在Grokking Cocoa博客上的文章提供了执行此操作的所有信息。然而,接下来并没有立即取得成功,需要进行一些调整。

To be able to debug those tests it is required to create a custom executable that will call those tests. The article by Sean Miceli on the Grokking Cocoa blog provides all the information to do this. Following it however did not yield immediate success and needed some tweaking.

我将介绍Sean教程中提供的一些for dummies大纲中的主要步骤时间弄清楚:

I will go over the main steps presented in Sean's tutorial providing some "for dummies" outline which took me some time to figure out:


  1. 设置一个包含单元测试但不运行它们的目标

  2. 设置otest可执行文件以运行测试

  3. 设置otest环境变量,以便otest可以找到您的单元测试

以下是使用 XCode 3.2.5进行

在XCode 4中,可以直接调试单元测试。只需编写测试,将其作为测试之一添加到目标中,并在其中设置断点。就这样。更多信息将来。

In XCode 4 it is possible to debug your unit tests DIRECTLY. Just write your test, add it to your target as one of the tests and set a breakpoint in it. That's all. More will come.


  1. 复制你的位于项目目标下的单元测试目标。这也将创建单元测试产品(.octest文件)的副本。在下图中,LogicTest是原始目标。

  2. 将单元测试目标和单元测试产品(.octest文件)重命名为相同的名称。在下图中,LogicTestsDebug是重复目标。

  3. 删除新目标的RunScript阶段

两者的名称可以是任何东西,但我会避免使用空格。

The name of both can be anything but I would avoid spaces.

最多这里重要的一点是获得正确的otest,即您当前的iOS而不是默认的Mac版本。这在Sean的教程中有详细描述。以下是一些有助于我正确设置的细节:

The most important point here is to get the correct otest, i.e. the one for your current iOS and not the default Mac version. This is well described in Sean's tutorial. Here are a few more details which helped me setting things right:


  1. Go Project-> New Custom Executable。这将弹出一个窗口,提示您输入可执行文件名和可执行文件路径。

  2. 输入您想要的任何名称。

  3. 复制粘贴iOS otest可执行文件的路径。在我的例子中,这是/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk/Developer/usr/bin/otest

  4. 按回车键。这将带您进入可执行文件的配置页面。

  5. 此时唯一要改变的是选择路径类型:相对于当前SDK。不要输入路径,这是在步骤3完成的。

  1. Go Project->New Custom Executable. This will pop open a window prompting you to enter an Executable Name and an Executable Path.
  2. Type anything you wish for the name.
  3. Copy paste the path to your iOS otest executable. In my case this was /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk/Developer/usr/bin/otest
  4. Press enter. This will bring you to the configuration page of your executable.
  5. The only thing to change at this point is to select "Path Type: Relative to current SDK". Do not type in the path, this was done at step 3.



步骤3 - 设置otest参数和环境变量



otest参数很容易设置...但事实证明这是我最大的问题。我最初将我的逻辑测试目标命名为LogicTests Debug。使用此名称和LogicTests Debug.octest(带引号)作为otest的参数,我一直使用退出代码1终止,并且永远不会停止进入我的代码......

Step 3 - Setting up the otest arguments and environment variables

The otest arguments are straightforward to setup... But this proved to be my biggest problem. I initially had named my logic test target "LogicTests Debug". With this name and "LogicTests Debug.octest" (with quotes) as argument to otest I kept having otest terminating with exit code 1 and NEVER stopping into my code...

解决方案:目标名称中没有空格!

The solution: no space in your target name!

otest的参数是:

The arguments to otest are:


  1. -SenTest Self(或全部或测试名称 - 在终端中键入man otest以获取列表)

  2. {LogicTestsDebug} .octest - Where {LogicTestsDebug}需要替换为您的逻辑测试包名称。

以下是复制/粘贴的环境变量列表:

Here is the list of environment variables for copy/pasting:


  • DYLD_ROOT_PATH:$ SDKROOT

  • DYLD_FRAMEWORK_PATH:$ {BUILD_PRODUCTS_DIR}:$ {SDK_ROOT}:$ {DYLD_FRAMEWORK_PATH}

  • IPHONE_SIMULATOR_ROOT:$ SDKROOT

  • CFFIXED_USER_HOME:$ {HOME} / Library / Application Support / iPhone Simulator / User

  • DYLD_LIBRARY_PATH:$ {BUILD_PRODUCTS_DIR}:$ {DYLD_LIBRARY_PATH}

  • DYLD_NEW_LOCA L_SHARED_REGIONS:是

  • DYLD_NO_FIX_PREBINDING:是

  • DYLD_ROOT_PATH: $SDKROOT
  • DYLD_FRAMEWORK_PATH: "${BUILD_PRODUCTS_DIR}: ${SDK_ROOT}:${DYLD_FRAMEWORK_PATH}"
  • IPHONE_SIMULATOR_ROOT: $SDKROOT
  • CFFIXED_USER_HOME: "${HOME}/Library/Application Support/iPhone Simulator/User"
  • DYLD_LIBRARY_PATH: ${BUILD_PRODUCTS_DIR}:${DYLD_LIBRARY_PATH}
  • DYLD_NEW_LOCAL_SHARED_REGIONS: YES
  • DYLD_NO_FIX_PREBINDING: YES

请注意,我也尝试过DYLD_FORCE_FLAT_NAMESPACE但这只是使otest崩溃。

Note that I also tried the DYLD_FORCE_FLAT_NAMESPACE but this simply made otest crash.

要运行您的otest可执行文件并开始调试您需要:

To run your otest executable and start debugging your tests you need to:


  1. 将您的有效目标设置为您的单位测试目标(在我的情况下为LogicTestsDebug)

  2. 设置您的活动可执行文件到您的otest可执行文件

您可以使用断点构建和运行可执行文件并调试测试。

You can build and run your executable and debug your tests with breakpoints.

作为附注,如果您在运行otest可执行文件时遇到问题,则可能与以下内容有关:

As a side note if you are having problems running your otest executable it can be related to:


  1. 错误路径。我最初有很多问题,因为我指的是mac otest。我一直在使用终止代码6进行启动时崩溃。

  2. 错误的参数。在我从bundle(.octest)名称中删除空格之前,我一直在使用退出代码1进行崩溃。

  3. 环境变量中的路径错误。肖恩教程有很多后续问题,可以提供一些其他人尝试过的见解。我现在的设置似乎工作,所以我建议你从这开始。

你可能会在控制台中收到一些可能导致的消息你认为你的环境变量有问题。您可能会注意到有关CFP参考的消息。此消息不会阻止测试正常运行,因此如果您在运行otest时遇到问题,请不要关注它。

You may get some message in the console which might lead you to think something is wrong with your environment variables. You may notice a message regarding CFPreferences. This message is not preventing the tests from running properly so don't focus on it f you have problems running otest.

上次一切正常后,您将可以在测试中的断点处停止。

Last once everything is working you will be able to stop at breakpoints in your tests.

我在很多博客上都看到集成XCode SenTestKit的主要限制是构建应用程序时无法运行测试。事实证明这实际上很容易管理。您只需将Logic测试包添加为应用程序项目的依赖项。这将确保构建逻辑测试包,即在构建应用程序之前运行所有测试。

I've read on many blogs that the main limitation of the integrated XCode SenTestKit is that tests cannot be run while building the application. Well as it turns out this is in fact quite easy to manage. You simply need to add your Logic tests bundle as a dependency to your application project. This will make sure your logic tests bundle is built, i.e. all tests are run, before your application is built.

为此,您可以拖放逻辑测试包到您的应用程序目标。

To do this you can drag and drop your logic test bundle onto your application target.

推荐答案

本文旨在作为操作方法而非真实问题。因此,这个答案只是为了让我将操作方法标记为已回答。这可能会被社区标记为不规则。我想知道在哪里发布未来的操作方法文章。

This post is intended as a "How-to" more than a real question. Therefore this answer is just meant to allow me to mark the "How-to" as "answered". This will probably be flagged by the community as irregular. I'm up for suggestions on where to post future "How-to" articles.

关于这个主题的最后一点说明。
对于那些仍然怀疑写单元测试是否值得的人我肯定会说是!

One final note though on this topic. For those who still wonder whether writing unit tests is worth it I would definitely say Yes!

我目前正在编写一个带有CoreData的应用程序并从中检索数据一个Web服务(xml解析)。完整的模型可以进行测试和调试,而无需:

I am currently writing an application with CoreData and retrieval of data from a web service (xml parsing). The complete model can be tested and debugged without having to:


  1. 在模拟器或设备上运行实际应用程序。不必使用该设备来运行测试是一个巨大的时间。这是每次运行2分钟和5秒之间的差异。

  2. 在测试模型时无需创建视图或控制器。完整的开发和测试只能在第一次迭代中关注模型。一旦模型被清除以进行集成,其余的开发可以遵循。

要调试xml解析,我可以简单地使用hard-编码我完全控制的文件。

To debug the xml parsing I can simply use "hard-coded" files which I completely control.

当你在代码中实现功能时,关键是编写测试。在整个应用程序的调试方面,它确实节省了时间。

The crux is of course to write the tests as you implement features in the code. It really is a time saver down the line in terms of debugging of the complete application.

Voilà,我会留下它。

Voilà, I'll leave it at that.

这篇关于如何运行和调试iPhone应用程序的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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