xcode 如何自动对每个构建执行单元测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6673333/
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
How do I automatically perform unit tests on each build?
提问by David
How do I automatically perform unit tests on each build?
如何自动对每个构建执行单元测试?
I tried to add the Unit Tests target to the Project Target as a dependency, but that doesn't seem to run the actual tests.
我尝试将单元测试目标作为依赖项添加到项目目标,但这似乎并没有运行实际测试。
采纳答案by Mark Szymczyk
Set the Test After Build build setting to Yes. Choose Product > Build For > Build For Testing to build the project and run the tests.
将构建后测试设置设置为是。选择 Product > Build For > Build For Testing 以构建项目并运行测试。
Xcode 5 Update
Xcode 5 更新
Xcode 5 does not support the Test After Build build setting. I don't know of any way to automatically run tests on each build in Xcode 5. From the OS X menu bar choose Product > Test or press Command + u to run unit tests in Xcode 5.
Xcode 5 不支持 Test After Build 构建设置。我不知道有什么方法可以在 Xcode 5 中的每个构建上自动运行测试。从 OS X 菜单栏中选择 Product > Test 或按 Command + u 在 Xcode 5 中运行单元测试。
回答by Jon Reid
To establish the relationship between your targets:
要建立目标之间的关系:
- In the Scheme picker, select "Edit Scheme…"
- Select the Test phase
- Make sure you're seeing the Info tab, not the Arguments tab
- Click '+' and specify your testing target
- You should see a list of tests. Click 'OK'
- 在方案选择器中,选择“编辑方案...”
- 选择测试阶段
- 确保您看到的是“信息”选项卡,而不是“参数”选项卡
- 单击“+”并指定您的测试目标
- 您应该会看到测试列表。点击“确定”
Then to run the tests, choose Product > Test, or simply ?U from the keyboard. This will:
然后要运行测试,请选择“产品”>“测试”,或者直接从键盘上选择 ?U。这会:
- Build your main target
- Build your test target
- Execute your main target according to its type, running the tests
- 建立你的主要目标
- 构建您的测试目标
- 根据类型执行主要目标,运行测试
回答by Brian Wigginton
Here's what I had to do to get XCode 4 to automatically run unit tests on build.
这是我必须做的才能让 XCode 4 在构建时自动运行单元测试。
- Enable "Test After Build" for your main target. (See Mark Szymczyk's answer)
- Enable your Test target to run during the build phase of your main target's build scheme.
- Click Product > Manage Schemesmenu item
- Double click your target's scheme (I only had one in my list)
- Unfold the Build accordionon the left
- Click the Build optionin the accordion
- You should see you test project on the right, make sure the Run checkbox is clicked.
- 为您的主要目标启用“构建后测试”。(见 Mark Szymczyk 的回答)
- 使您的测试目标能够在主目标构建方案的构建阶段运行。
- 单击产品 > 管理方案菜单项
- 双击您的目标方案(我的列表中只有一个)
- 展开左侧的 Build 手风琴
- 单击手风琴中的“构建”选项
- 您应该会在右侧看到您的测试项目,请确保单击“运行”复选框。
回答by onaquest
I thought I'd give a high-levelexplanation of the steps that worked for me. (OS X 10.7.4, Xcode 4.3.3) If you are unfamiliar with things like Build Phases and Schemes, here's a great resource from Apple: http://developer.apple.com/library/ios/#documentation/ToolsLanguages/Conceptual/Xcode4UserGuide/Building/Building.html
我想我会对适合我的步骤给出一个高层次的解释。(OS X 10.7.4,Xcode 4.3.3)如果您不熟悉诸如构建阶段和方案之类的东西,这里有来自 Apple 的一个很好的资源:http: //developer.apple.com/library/ios/#documentation/ToolsLanguages/概念/Xcode4UserGuide/Building/Building.html
For a new project, make sure you check the "Include Unit Tests" checkbox on the page where you enter the name of the new project. Click on the project icon in the Project Navigator to see the two targets that have been created automatically. They'll be called something like "MyCocoaApp" and "MyCocoaAppTests". Select the MyCocoaAppTests target (NOT the MyCocoaApp one!) and select "Build Settings" from the choices at the top of the view. Enter "test after build" in the search bar near the top of the view. You should see a single setting for "Test After Build" and it's set to NO by default. Click on the NO setting and change it to YES. One more step. Bring up the scheme for the project. (Product Menu -> Edit Scheme...) Select "Build" in the column on the left. You should see your two targets and some check boxes to the right. On the line for "MyCocoaAppTests", check the "Run" checkbox and hit "OK". You're done. Type Command-B. Your app will build, the unit tests will build, the unit tests will run and fail on an error that is there by default.
对于新项目,请确保在输入新项目名称的页面上选中“包括单元测试”复选框。单击 Project Navigator 中的项目图标以查看已自动创建的两个目标。它们将被称为“MyCocoaApp”和“MyCocoaAppTests”之类的东西。选择 MyCocoaAppTests 目标(不是 MyCocoaApp 目标!)并从视图顶部的选项中选择“Build Settings”。在视图顶部附近的搜索栏中输入“构建后测试”。您应该会看到“构建后测试”的单一设置,并且默认设置为 NO。单击 NO 设置并将其更改为 YES。再一步。提出项目方案。(产品菜单 -> 编辑方案...)在左侧的列中选择“构建”。您应该会在右侧看到两个目标和一些复选框。在“MyCocoaAppTests”行上,选中“Run”复选框并点击“OK”。你完成了。键入 Command-B。您的应用程序将构建,单元测试将构建,单元测试将运行并在默认情况下出现错误时失败。
If you have to add unit testing to an existing project, it's a bit more involved. First, follow these instructions by Apple: https://developer.apple.com/library/mac/#documentation/developertools/Conceptual/UnitTesting/02-Setting_Up_Unit_Tests_in_a_Project/setting_up.html#//apple_ref/doc/uid/TP40002143-CH3-SW1
如果您必须向现有项目添加单元测试,则涉及更多。首先,按照 Apple 的这些说明进行操作:https: //developer.apple.com/library/mac/#documentation/developertools/Conceptual/UnitTesting/02-Setting_Up_Unit_Tests_in_a_Project/setting_up.html#//apple_ref/doc/uid/TP40002143-CH3 -SW1
If you don't miss any steps, you should now have unit tests set up that run when you select (Product Menu -> Test). Now, do the two steps described above: 1) Set the "Test After Build" setting to YES for the MyCocoaAppTest target; 2) Check the "Run" checkbox for the MyCocoaAppTest target in the Build section of the Scheme for the app. LAST STEP: You have to add a new "Run Script" build phase to the MyCocoaAppTest target (NOT the MyCocoaApp one!). Select the test target, click on "Build Phases", click on the "Add Build Phase" icon in the lower right, select "Add Run Script". A "Run Script" section will open up below the other build phases. The script field has this text in it: "Type a script or drag a script file from your workspace". In that field, enter: "${SYSTEM_DEVELOPER_DIR}/Tools/RunUnitTests". (Including the quotation marks.)
如果您没有遗漏任何步骤,您现在应该设置在您选择(产品菜单 -> 测试)时运行的单元测试。现在,执行上述两个步骤: 1) 将 MyCocoaAppTest 目标的“Test After Build”设置为 YES;2) 在应用程序的“构建”部分中选中 MyCocoaAppTest 目标的“运行”复选框。最后一步:您必须向 MyCocoaAppTest 目标(不是 MyCocoaApp 目标!)添加一个新的“运行脚本”构建阶段。选择测试目标,点击“Build Phases”,点击右下角的“Add Build Phase”图标,选择“Add Run Script”。“运行脚本”部分将在其他构建阶段下方打开。脚本字段中包含以下文本:“键入脚本或从工作区拖动脚本文件”。在该字段中,输入:“${SYSTEM_DEVELOPER_DIR}/Tools/RunUnitTests”。(包括引号。)
That's it. Hit Command-B. Your app should build. Your tests should build and then run. Good luck. Phew.
就是这样。按 Command-B。您的应用程序应该构建。您的测试应该构建然后运行。祝你好运。呼。