Adding Unit Tests to an already existing project?

2019-01-13 12:45发布

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?

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.

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.

5条回答
手持菜刀,她持情操
2楼-- · 2019-01-13 13:06

I think you can do this in this way:

  1. Open your Xcode project
  2. Open targets
  3. Click "Add Target" button
  4. Click "Other"
  5. Select "Cocoa Touch Unit Test Testing Bundle"
查看更多
Bombasti
3楼-- · 2019-01-13 13:06

Xcode 7 update

Method one

File > New > Target...

Choose iOS Unit Testing Bundle. (If you want to add UI Testing then choose the UI Testing Bundle.)

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.

enter image description here

The test targets in this image have already been added, but you can see where the add button is.

Related

查看更多
forever°为你锁心
4楼-- · 2019-01-13 13:07

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.

Change the bundle id on a test target to the correct one, if needed, by clicking the target -> Info -> Bundle Identifier.

Press CMD + U and your tests will run.

If you use CocoaPods (which is likely), you should also add Pods to the Configurations of your Project.

enter image description here

查看更多
做个烂人
5楼-- · 2019-01-13 13:12

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:

Run tests with cmd-U in xcode

查看更多
Anthone
6楼-- · 2019-01-13 13:19
登录 后发表回答