xcode 使用 iOS 实现单元测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2002330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:02:25  来源:igfitidea点击:

Implementing Unit Testing with iOS

iosxcodeunit-testingios-simulator

提问by ingh.am

I've followed this tutorialto setup unit testing on my app when I got a little stuck.

当我有点卡住时,我已经按照本教程在我的应用程序上设置单元测试。

At bullet point 8 in that tutorial it shows this image, which is what I should be expecting when I build: alt text http://developer.apple.com/library/ios/documentation/Xcode/Conceptual/iphone_development/art/logic_test_failure_text_editor.jpg

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

However this isn't what I get when I build. I get this error message: Command /bin/sh failed with exit code 1as well as the error message the unit test has created. Then, when I expand on the first error I get this:

然而,这不是我在构建时得到的。我收到此错误消息: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

Now this is very odd as it is running the tests (and succeeding as you can see my STFail firing) because if I add a different test which passes I get no errors, so the tests are working fine. But why am I getting this extra build fail?

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

It may also be of note that when downloading solutions/templates which should work out the box, I get the same error. I'm guessing I've set something up wrong here but I've just followed a tutorial 100% correctly!

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

EDIT: According to this blog, this postand a few other websites, I'm not the only one getting this problem. It has been like this since the release of xCode 3.2, assuming the apple dev center documents and tutorials etc are pre-3.2 as well.

编辑:根据这篇博客这篇文章和其他一些网站,我不是唯一遇到这个问题的人。自 xCode 3.2 发布以来一直是这样,假设苹果开发中心的文档和教程等也是 3.2 之前的。

However some say its a known issue whereas others seem to think this was intentional. I for one would like both the extended console and in code messages, and I certainly do not like the "Command /bin/sh..." error and really think they would have documented such an update. Hopefully it will be fixed soon anyway.

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

UPDATE: Here's confirmation it's something changed since the release of xCode 3.2.1.

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

This image: alt text http://ing0.co.uk/info/pics/unittest-xcode-3.2.1.pngis from my test build using 3.2.1. This one is from an older version (3.1.4): alt text http://ing0.co.uk/info/pics/unittest-xcode-3.1.4.png. (The project for both was unchanged).

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

回答by Anton

Double click 'Run script' under 'Targets'/'Your script target name' then type

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

# Run the unit tests in this test bundle. 
"${SYSTEM_DEVELOPER_DIR}/Tools/RunUnitTests" 1> /tmp/RunUnitTests.out

Basically you need to append

基本上你需要追加

1> /tmp/RunUnitTests.out

to what is already there

已经存在的东西

回答by Joe Yang

try to check this issue SenTestCase in Xcode 3.2 and XCBuildLogCommandInvocationSection Errors

尝试在 Xcode 3.2 和 XCBuildLogCommandInvocationSection 错误中检查此问题SenTestCase

the workaround in that question work for me.

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

回答by Thomas Joulin

Have you tried to set the build configuration to "Debug" for the test case ?

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

i did that, as shown at http://nothing2fancy.com/2009/08/04/failed-tests-for-architecture-i386-gc-off/and it worked

我这样做了,如http://nothing2fancy.com/2009/08/04/failed-tests-for-architecture-i386-gc-off/所示,它起作用了

回答by Akku

Menu: Project --> New Build Phase --> New RunScript Build Phase --> Paste the following into the empty text area:

菜单:项目 --> 新建构建阶段 --> 新建 RunScript 构建阶段 --> 将以下内容粘贴到空白文本区域:

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

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

Now try to build and you should get an error on failed unit tests.

现在尝试构建,你应该在失败的单元测试中得到一个错误。

回答by lufutu

I don't know it a right way or not, but it's work for me.

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

In General Tab of info Test Case, I add my main application executable target as a direct dependency.

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

And no error for ‘i386′ (GC OFF)”.

“i386”(GC 关闭)没有错误。

Good luck.

祝你好运。

回答by Hyman Creigh

When you create your firt test case classes, make sure the Target is set to your UnitTest target, not your application target.

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

If you forget to do this when you create the class, you can "Get Info" on the existing class, and set the target there....

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

回答by Kay

OK, never played with unit tests in XCode (of course I liked JUnit in good old java times). So it is just a trial: What compiler are you using, GCC or LLVM? Maybe GCC is the better choice to be on the safe side.

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

Do you have the same behaviour when "Build Active Architecture Only" in Project settings is enabled? This switch removes duplicate error messages when building for armv6 and armv7, maybe it's related.

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

回答by Madhup Singh Yadav

Please have a look at apple's latest sample. When you download the code there is a read me file in which its clearly written:

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

———————————————————————————————————————————————————————————————————————————————
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.

———————————————————————————————————————————————————————————————————————————————

Hence there is some change in the sentesting kit. I don't think it would be worth for filing a bug.

因此,sentesting 工具包有一些变化。我认为提交错误不值得。

回答by Radu Cojocaru

That is the expected behavior. The build will fail if one of the tests fail and the build will succeed if all the tests pass.

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