ios 将单元测试添加到现有项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6865735/
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
Adding Unit Tests to an already existing project?
提问by Randall
When you create an XCode 4 project, it asks you if you want to include unit testing. But how do you add it to a project that's been created without them?
当您创建 XCode 4 项目时,它会询问您是否要包括单元测试。但是如何将它添加到没有它们创建的项目中呢?
I started a project with Unit Tests to try to figure it out, and it looks like the only difference between it and my existing project are the Test.h and Test.m files in the new project.
我用单元测试开始了一个项目,试图弄清楚它和我现有项目之间的唯一区别是新项目中的 Test.h 和 Test.m 文件。
But when I move them into my old project and try to build, it says no such file or directory: SenTestingKit/SenTestingKit.h. That looks like a framework to me, but if I go to build phases and try to add the framework, there isn't one available called SenTestingKit :( And the new project only links the usual suspects: UIKit, CoreGraphics, and Foundation, but no SenTestingKit.
但是当我将它们移动到我的旧项目并尝试构建时,它说没有这样的文件或目录:SenTestingKit/SenTestingKit.h。对我来说,这看起来像是一个框架,但是如果我去构建阶段并尝试添加框架,则没有可用的 SenTestingKit :( 而新项目只链接通常的嫌疑人:UIKit、CoreGraphics 和 Foundation,但是没有 SenTestingKit。
回答by akashivskyy
Answer updated for Xcode 10.2.
为 Xcode 10.2 更新了答案。
Glad you asked this question — it's never too late to add tests to a project!
很高兴你问了这个问题——向项目添加测试永远不会太晚!
- Open your targets panelby selecting your project in the navigator,
- Click the + buttonthat's at the bottom of the target list,
- Select iOS Unit Testing Bundleunder Test section in iOS tab,
- Provide the required information and click Done.
- 通过在导航器中选择您的项目来打开您的目标面板,
- 单击目标列表底部的 + 按钮,
- 在 iOS 选项卡的 Test 部分下选择iOS Unit Testing Bundle,
- 提供所需信息并单击完成。
It's that simple. Xcode will generate a target, some boilerplate files such as Info.plist and an initial test case file for you. Happy testing!
就这么简单。Xcode 将为您生成一个目标、一些样板文件(例如 Info.plist)和一个初始测试用例文件。测试愉快!
回答by Lee Probert
Also, if you want to be able to do cmd-U to run your tests then delete the scheme that was created for the test bundle and instead edit your main application scheme and add the test bundle in the Test configuration. See this screenshot:
此外,如果您希望能够执行 cmd-U 来运行您的测试,请删除为测试包创建的方案,而是编辑您的主应用程序方案并在测试配置中添加测试包。看这个截图:
回答by Suragch
Xcode 7 update
Xcode 7 更新
Method one
方法一
File > New > Target...
文件 > 新建 > 目标...
Choose iOS Unit Testing Bundle. (If you want to add UI Testing then choose the UI Testing Bundle.)
选择 iOS 单元测试包。(如果您想添加 UI 测试,请选择 UI 测试包。)
Method two
方法二
Click your project name in the Project Navigator. You will see TARGETS listed. At the bottom of the screen press the plus (+) button and add the iOS Unit Testing Bundle.
在项目导航器中单击您的项目名称。您将看到列出的目标。在屏幕底部按加号 (+) 按钮并添加 iOS 单元测试包。
The test targets in this image have already been added, but you can see where the add button is.
此图中的测试目标已添加,但您可以看到添加按钮的位置。
Related
有关的
回答by jki
This is pretty descriptive how-to guide: Adding Unit Tests to an existing iOS project with Xcode 4
这是非常具有描述性的操作指南: 使用 Xcode 4 将单元测试添加到现有的 iOS 项目
回答by yoAlex5
One more variant using Test navigator
使用测试导航器的另一种变体
Xcode version 10.2.1
Xcode 版本 10.2.1
- Open
Test navigator
(? Command+ 6) - +at the bottom
New Unit Test Target...
- Choose options
- 打开
Test navigator
( ? Command+ 6) - +在底部
New Unit Test Target...
- 选择选项
Do not forget add next import to test internal
[About]elements
不要忘记添加下一个导入来测试internal
[About]元素
#import "myClass.h"
for Objective-C@testable import module_name
for Swift
#import "myClass.h"
对于 Objective-C@testable import module_name
为斯威夫特
回答by Swan
[Updated for Xcode 10.2]:
[针对 Xcode 10.2 更新]:
- Open your Xcode project
- Open targets
- Click "Add Target" button
- Go to "Test" section
- Select "iOS Unit Testing Bundle"
- 打开你的 Xcode 项目
- 开放目标
- 单击“添加目标”按钮
- 转到“测试”部分
- 选择“iOS 单元测试包”
回答by Denis Kutlubaev
Some more tips to the correct answer:
关于正确答案的更多提示:
In XCode 6
it is much easier now since you don't need to fix any build settings.
在XCode 6
它是非常容易,因为现在你不需要解决任何构建设置。
Change the bundle id on a test target to the correct one, if needed, by clicking the target -> Info
-> Bundle Identifier
.
如果需要,通过单击目标 -> Info
->将测试目标上的包 ID 更改为正确的Bundle Identifier
。
Press CMD + U
and your tests will run.
按CMD + U
,您的测试将运行。
If you use CocoaPods (which is likely), you should also add Pods to the Configurations of your Project.
如果您使用 CocoaPods(很有可能),您还应该将 Pod 添加到您的项目的配置中。