Adding Unit Tests to an already existing project?

前端 未结 7 1401
不知归路
不知归路 2020-12-02 16: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

相关标签:
7条回答
  • 2020-12-02 17:01

    One more variant using Test navigator

    Xcode version 10.2.1

    1. Open Test navigator(⌘ Command + 6)
    2. + at the bottom
    3. New Unit Test Target...
    4. Choose options

    Do not forget add next import to test internal[About] elements

    • #import "myClass.h" for Objective-C
    • @testable import module_name for Swift
    0 讨论(0)
  • 2020-12-02 17:02

    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

    0 讨论(0)
  • 2020-12-02 17:09

    This is pretty descriptive how-to guide: Adding Unit Tests to an existing iOS project with Xcode 4

    0 讨论(0)
  • 2020-12-02 17:14

    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!

    0 讨论(0)
  • 2020-12-02 17:15

    [Updated for Xcode 10.2]:

    1. Open your Xcode project
    2. Open targets
    3. Click "Add Target" button
    4. Go to "Test" section
    5. Select "iOS Unit Testing Bundle"
    0 讨论(0)
  • 2020-12-02 17:18

    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.

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

    Related

    • How to do a Unit Test in Xcode
    • Xcode UI Test example
    0 讨论(0)
提交回复
热议问题