Adding Unit Tests to an already existing project? Adding Unit Tests to an already existing project? xcode xcode

Adding Unit Tests to an already existing project?


Answer updated for Xcode 10.2.

Glad you asked this question — it's never too late to add tests to a project!

  1. Open your targets panel by selecting your project in the navigator,
  2. Click the + button that's at the bottom of the target list,
  3. Select iOS Unit Testing Bundle under Test section in iOS tab,
  4. Provide the required information and click Done.

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!


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


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