AppCode 2019.2 Help

Create tests

AppCode provides templates for creating unit and UI tests based on the XCTest framework. When creating a new project in AppCode, you can select if you want unit and UI tests to be added. In this case, AppCode creates test targets containing test classes with initial content. However, you can add test targets and test classes anytime after a project is created.

To quickly add stub code for methods in test classes, use the Generate menu ⌘N.

Add a test target

  1. Open the project settings by pressing ⌘; or selecting File | Project Settings from the main menu.

  2. Click the Add button and select a test target from the dialog that opens.

    Add a test target
    Select iOS UI Testing Bundle to create a target for UI tests or iOS Unit Testing Bundle — for unit tests. Click Next.

  3. On the next page, if necessary, change the values in the following fields:
    • Product Name: the name of the new test target.

    • Organization Name: the name of your company or your name.

    • Language: the programming language for the test classes within the target.

    • Project: the project where the new target will be created.

    • Target to be Tested: the target that will be tested.

    Add a test class

A new test target will be added to the project. This target contains a test class with stub code for several test methods that you can fill in.

Added test target

Add a test class

  1. In the Project tool window, right-click the group you want to add a test class to and select New | File from Xcode Template.

  2. Select the Source file type from the left pane and UI Test Case Class or Unit Test Case Class from the right pane. Click Next.

    Add a new test class

  3. On the next page, if necessary, change the values in the following fields:
    • File name: the name of the new test class.

    • Location: the directory in the file system where the new file will be created.

    • Subclass of: the parent class for the class that is being created. By default, XCTestCase.

    • Language: the programming language the class will be written in.

    • Group: the group in the AppCode project the class will belong to.

    • Target: the list of targets where the class will be added.

    Add a test class

Generate code for tests

To generate code for test methods, place the caret within the test class code, press ⌘N, and select the desired action from the menu that opens.

Depending on the testing framework you use, different code templates can be generated:

  • XCTest and Google Test: SetUp, TearDown, and Test methods:
    Generate test methods
    For Google Test, AppCode also offers you a variety of TEST macros:
    • TEST()
    • TEST_F()
    • TEST_P()
    • TYPED_TEST()
    • TYPED_TEST_P()

    You can generate a TEST() macro, and after you specify a suite name, AppCode will change this macro to the correct one automatically.

  • Kiwi: it, context, beforeEach, afterEach, and specify blocks:

    Code Generation menu for Kiwi

Last modified: 10 October 2019