【问题标题】:Implementing Unit Testing with iOS使用 iOS 实施单元测试
【发布时间】:2011-01-01 10:04:51
【问题描述】:

当我遇到一点困难时,我已关注 this tutorial 在我的应用程序上设置单元测试。

在该教程的第 8 点,它显示了这张图片,这是我在构建时应该期待的:alt text http://developer.apple.com/library/ios/documentation/Xcode/Conceptual/iphone_development/art/logic_test_failure_text_editor.jpg

但是,这不是我在构建时得到的。我收到此错误消息:Command /bin/sh failed with exit code 1 以及单元测试创​​建的错误消息。然后,当我扩展第一个错误时,我得到了这个:

PhaseScriptExecution "Run Script" "build/3D Pool.build/Debug-iphonesimulator/LogicTests.build/Script-1A6BA6AE10F28F40008AC2A8.sh"
cd "/Users/james/Desktop/FYP/3D Pool"
setenv ACTION build
setenv ALTERNATE_GROUP staff
...
setenv XCODE_VERSION_MAJOR 0300
setenv XCODE_VERSION_MINOR 0320
setenv YACC /Developer/usr/bin/yacc
/bin/sh -c "\"/Users/james/Desktop/FYP/3D Pool/build/3D Pool.build/Debug-iphonesimulator/LogicTests.build/Script-1A6BA6AE10F28F40008AC2A8.sh\""

/Developer/Tools/RunPlatformUnitTests.include:412: note: Started tests for architectures 'i386'
/Developer/Tools/RunPlatformUnitTests.include:419: note: Running tests for architecture 'i386' (GC OFF)
objc[12589]: GC: forcing GC OFF because OBJC_DISABLE_GC is set
Test Suite '/Users/james/Desktop/FYP/3D Pool/build/Debug-iphonesimulator/LogicTests.octest(Tests)' started at 2010-01-04 21:05:06 +0000
Test Suite 'LogicTests' started at 2010-01-04 21:05:06 +0000
Test Case '-[LogicTests testFail]' started.
/Users/james/Desktop/FYP/3D Pool/LogicTests.m:17: error: -[LogicTests testFail] : Must fail to succeed.
Test Case '-[LogicTests testFail]' failed (0.000 seconds).
Test Suite 'LogicTests' finished at 2010-01-04 21:05:06 +0000.
Executed 1 test, with 1 failure (0 unexpected) in 0.000 (0.000) seconds

Test Suite '/Users/james/Desktop/FYP/3D Pool/build/Debug-iphonesimulator/LogicTests.octest(Tests)' finished at 2010-01-04 21:05:06 +0000.
Executed 1 test, with 1 failure (0 unexpected) in 0.000 (0.002) seconds

/Developer/Tools/RunPlatformUnitTests.include:448: error: Failed tests for architecture 'i386' (GC OFF)
/Developer/Tools/RunPlatformUnitTests.include:462: note: Completed tests for architectures 'i386'
Command /bin/sh failed with exit code 1

现在这很奇怪,因为它正在运行测试(并且成功,因为您可以看到我的 STFail 触发),因为如果我添加一个通过的不同测试,我不会收到任何错误,所以测试工作正常。但是为什么我会得到这个额外的构建失败?

还可能需要注意的是,在下载应该可以解决问题的解决方案/模板时,我会遇到同样的错误。我猜我在这里设置了一些错误,但我只是 100% 正确地遵循了一个教程!

编辑:根据this blogthis post 和其他一些网站,我不是唯一遇到这个问题的人。自从 xCode 3.2 发布以来就是这样,假设苹果开发中心的文档和教程等也是 pre-3.2。

但是,有些人说这是一个已知问题,而其他人似乎认为这是故意的。我想要扩展控制台和代码消息,我当然不喜欢“命令/bin/sh ...”错误,并且真的认为他们会记录这样的更新。无论如何,希望它会尽快修复。

更新: 这是确认自 xCode 3.2.1 发布以来发生了一些变化。

这张图片: alt text http://ing0.co.uk/info/pics/unittest-xcode-3.2.1.png 来自我使用 3.2.1 的测试版本。这个来自旧版本(3.1.4): alt text http://ing0.co.uk/info/pics/unittest-xcode-3.1.4.png。 (两者的项目都没有改变)。

【问题讨论】:

  • 我在 3.2.5(64 位 Intel Core i3,OSX 10.6.6)中遇到了同样的问题
  • 很高兴你赏金,但我不太确定会有答案!
  • 别过了,我似乎有测试工作。

标签: ios xcode unit-testing ios-simulator


【解决方案1】:

双击“目标”/“您的脚本目标名称”下的“运行脚本”,然后键入

# 在这个测试包中运行单元测试。 "${SYSTEM_DEVELOPER_DIR}/Tools/RunUnitTests" 1> /tmp/RunUnitTests.out

基本上你需要追加

1> /tmp/RunUnitTests.out

已经存在的东西

【讨论】:

  • 恐怕看起来像死链接!
  • 太棒了,这为我解决了问题(直到现在这让我发疯!)
  • 也适用于我,但我使用了“1> /dev/null”,因为无论如何都不会查看 tmp 文件中的输出。
【解决方案2】:

您是否尝试将测试用例的构建配置设置为“调试”?

我这样做了,如http://nothing2fancy.com/2009/08/04/failed-tests-for-architecture-i386-gc-off/ 所示,并且成功了

【讨论】:

  • 感谢您的建议,但我的情况并非如此!单元测试本身工作正常,这是应该出现在代码中的消息已经从最新版本的 xcode 中消失了,它们现在出现在构建结果中,但我认为这不是故意的!你使用的是哪个版本的 xcode?​​span>
  • 版本 3.2.2 预发布。这样可能会在最新版本中修复
  • 可能,我还在使用 3.2.1。谢谢你,我会得到当前的预发布版本并试一试。
  • 嗯,昨天发布了3.2 SDK。我没有再次尝试单元测试,但由于它在预发布版本中工作,我非常有信心它会在稳定版本上工作;)
【解决方案3】:

尝试检查这个问题SenTestCase in Xcode 3.2 and XCBuildLogCommandInvocationSection Errors

该问题中的解决方法对我有用。

【讨论】:

    【解决方案4】:

    菜单:Project --> New Build Phase --> New RunScript Build Phase --> 将以下内容粘贴到空白文本区域:

    "${SYSTEM_DEVELOPER_DIR}/Tools/RunUnitTests" 1> /tmp/RunUnitTests.out

    现在尝试构建,您应该会在单元测试失败时收到错误消息。

    【讨论】:

      【解决方案5】:

      我不知道它是否正确,但它对我有用。

      在信息测试用例的常规选项卡中,我 添加我的主应用程序可执行文件 作为直接依赖的目标。

      “i386”(GC OFF)没有错误。

      祝你好运。

      【讨论】:

      • 感谢您的输入,但它仍然做同样的事情:(
      【解决方案6】:

      当您创建第一个测试用例类时,请确保将 Target 设置为您的 UnitTest 目标,而不是您的应用程序目标。

      如果您在创建类时忘记执行此操作,您可以在现有类上“获取信息”,并在那里设置目标....

      【讨论】:

      • 我已经有一段时间没有尝试这样做了,但我确定我做到了!
      【解决方案7】:

      好的,从来没有在 XCode 中使用过单元测试(当然我喜欢 Java 旧时代的 JUnit)。所以这只是一个试验:你使用的是什么编译器,GCC 还是 LLVM?也许 GCC 是安全方面的更好选择。

      在项目设置中启用“仅构建活动架构”时,您是否有相同的行为?此开关在为 armv6 和 armv7 构建时删除重复的错误消息,可能是相关的。

      【讨论】:

        【解决方案8】:

        请查看苹果最新的sample。当您下载代码时,有一个自述文件,其中清楚地写着:

        ———————————————————————————————————————————————————————————————————————————————
        Running Logic Tests
        To run the logic tests:
        1. Choose Project > Set Active Target > CalculatorTests.
        2. Choose Project > Set Active SDK > Simulator.
        3. Choose Build > Build. Xcode runs the test cases implemented in
           the CalculatorTests.m file.
        4. Choose Build > Build Results to open the Build Results window, containing
           the tests results. You may have to click the Show Transcript button (the
           third button on the bottom-left corner of the build results list) to view
           the build transcript.
        
        The logic-tests results look similar to this:
        
           PhaseScriptExecution <project_directory>/build/iPhoneUnitTests.build/Debug-iphonesimulator/CalculatorTests.build/Script-17AA84010F99894F00167681.sh
               cd <project_directory>
               /bin/sh -c <project_directory>/build/iPhoneUnitTests.build/Debug-iphonesimulator/CalculatorTests.build/Script-17AA84010F99894F00167681.sh
           /Developer/Tools/RunPlatformUnitTests.include:364: note: Started tests for architectures 'i386'
           /Developer/Tools/RunPlatformUnitTests.include:371: note: Running tests for architecture 'i386' (GC OFF)
           objc[1222]: GC: forcing GC OFF because OBJC_DISABLE_GC is set
           objc[1222]: GC: forcing GC OFF because OBJC_DISABLE_GC is set
           Test Suite '<project_directory>/build/Debug-iphonesimulator/CalculatorTests.octest(Tests)' started at 2009-05-19 16:55:28 -0700
           Test Suite 'CalculatorTests' started at 2009-05-19 16:55:28 -0700
           <time> otest[1222:80f] -[CalculatorTests testAddition] setUp
           <time> otest[1222:80f] -[CalculatorTests testAddition] start
           <time> otest[1222:80f] -[CalculatorTests testAddition] end
           <time> otest[1222:80f] -[CalculatorTests testAddition] tearDown
           Test Case '-[CalculatorTests testAddition]' passed (0.007 seconds).
           <time> otest[1222:80f] -[CalculatorTests testDivision] setUp
           <time> otest[1222:80f] -[CalculatorTests testDivision] start
           <time> otest[1222:80f] -[CalculatorTests testDivision] end
           <time> otest[1222:80f] -[CalculatorTests testDivision] tearDown
           Test Case '-[CalculatorTests testDivision]' passed (0.003 seconds).
           <time> otest[1222:80f] -[CalculatorTests testInputException] setUp
           <time> otest[1222:80f] -[CalculatorTests testInputException] start
           <time> otest[1222:80f] -[CalculatorTests testInputException] end
           <time> otest[1222:80f] -[CalculatorTests testInputException] tearDown
           ...
           Test Case '-[CalculatorTests testSubtractionNegativeResult]' passed (0.002 seconds).
           Test Suite 'CalculatorTests' finished at 2009-05-19 16:55:28 -0700.
           Executed 6 tests, with 0 failures (0 unexpected) in 0.021 (0.022) seconds
        
           Test Suite '<project_directory>/build/Debug-iphonesimulator/CalculatorTests.octest(Tests)' finished at 2009-05-19 16:55:28 -0700.
           Executed 6 tests, with 0 failures (0 unexpected) in 0.021 (0.024) seconds
        
           /Developer/Tools/RunPlatformUnitTests.include:388: note: Passed tests for architecture 'i386' (GC OFF)
           /Developer/Tools/RunPlatformUnitTests.include:399: note: Completed tests for architectures 'i386'
        
        
        Remember that logic tests are executed as part of the build process to provide
        you with build errors for failed unit tests. Logic unit-test bundles are not
        intented to run in iPhone Simulator or a device.
        
        ———————————————————————————————————————————————————————————————————————————————
        

        因此,检测工具包发生了一些变化。我认为提交错误不值得。

        【讨论】:

        • @ing0 抱歉,没注意到 :( 但是男孩!赏金是 7 天,我就是这样回答的 ;)
        【解决方案9】:

        这是预期的行为。如果其中一个测试失败,则构建将失败,如果所有测试都通过,则构建将成功。

        【讨论】:

        • 你显然没有阅读这篇文章。单元测试工作正常,但它们没有按预期显示。查看 3.2 版与 3.1 版的屏幕截图。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-06
        • 1970-01-01
        • 1970-01-01
        • 2019-11-23
        • 2018-10-02
        • 2017-09-10
        相关资源
        最近更新 更多