AppCode 2021.2 Help

Google Test

Google Test is a powerful unit testing framework for C++. In AppCode, you can easily run and debug tests, explore test results in a dedicated view, and use various code assistance features.

Install Google Test and add it to your project

Prerequisites

To compile the Google Test framework, you need to have CMake installed on your Mac. If you are using the Homebrew package manager, just run the brew install cmake command in the terminal.

Compile and install Google Test

  1. Clone the GoogleTest repository on your computer.

  2. In the googletest directory, create a new directory (for example, name it build), and run the cmake command from there to generate the build output:

    cd googletest mkdir build cd build cmake ../
  3. Compile and install Google Test:

    make sudo make install

Add Google Test to your project

The Google Test framework can be added from Xcode:

  1. Open your project in Xcode. You can do it from AppCode by selecting File | Open Project from Xcode from the main menu.

  2. Select the project in the project navigator, then select the target and open the Build Phases tab.

  3. In the Link binary with libraries section, select the generated libgtest.a file from usr/local/lib.

    Add a library in Xcode
  4. On the Build Settings tab, add the header and library search paths in the corresponding fields:

    • Header Search Path: /usr/local/include

    • Library Search Path: $(inherited) /usr/local/lib $(PROJECT_DIR)/

Generate tests

In the files with gtest included, you can generate code for tests and test fixtures using the Generate menu ⌘ N.

Generate Google Test

When called from a fixture, this menu additionally includes SetUp Method and TearDown Method:

Generate SetUp Method

Run tests

If you run Google Test as a regular application using the main method, the test results will be printed in a standard way on the console. In AppCode, you can run single tests or create a run/debug configuration to run all tests or an arbitrary set of tests. In this case, you will see the test results on a separate tab of the Run tool window.

Run a single test

Do one of the following:

  • Click the the Run button button in the gutter and select Run '<TestName>'.

  • Place the caret at the test declaration and press ⇧ F10.

Run a test fixture

Do one of the following:

  • Click the the Run All button button in the gutter and select Run '<TestFixtureName>'.

  • Place the caret at the fixture declaration and press ⇧ F10.

Run tests with a run/debug configuration

  1. Create a run/debug configuration for Google Test.

  2. Select a created configuration in the toolbar and click the Run button or press ⇧ F10.

Google Test run/debug configuration

Although Google Test provides the main() entry, and you can run tests as regular applications, we recommend using the dedicated Google Test run/debug configuration. It includes test-related settings and let you benefit from the built-in test runner, which is unavailable if you run tests as regular programs.

  1. To create a Google Test configuration, go to Run | Edit Configurations, click Icons general add and select Google Test from the list of templates.

  2. Specify the test or suite to be included in the configuration, or provide a pattern for filtering test names. Auto-completion is available in the fields to help you quickly fill them up:

    completion in configuration fields

    Set wildcards to specify test patterns, for example:

    pattern for tests

    In other fields of the configuration settings, you can set environment variables and command line options. For example, use Program arguments field to pass the --gtest_repeat flag and run a Google test multiple times:

    flags in program arguments

    The output will look as follows:

    Repeating all tests (iteration 1) ... Repeating all tests (iteration 2) ... Repeating all tests (iteration 3) ...

  3. Save the configuration, and it's ready for RunIcons actions execute or DebugIcons actions start debugger.

Exploring results

You can see the results of the executed tests on a separate tab of the Run tool window:

Test results

On the left, you see the tree of tests, and on the right - error messages for not passed and skipped tests.

The test tree doesn't display successfully passed and skipped tests by default. To show them, click Icons run configurations toolbar passed or the Skipped Tests button on the toolbar, respectively:

Show results of passed and skipped tests

Test tree shows all the tests while they are being executed one by one. For parameterized tests, you will see the parameters in the tree as well. Also, the tree includes disabled tests (those with the DISABLED prefix in their names) and marks them as skipped with the corresponding icon.

Test runner for Google tests

Skipping tests at runtime

You can configure some tests to be skipped based on a condition evaluated at runtime. For this, use the GTEST_SKIP() macro.

Add the conditional statement and the GTEST_SKIP() macro to the test you want to skip:

TEST(Foo, Bar) { //... if (condition) GTEST_SKIP_("message"); // or GTEST_SKIP() with no message //... }

Use the Show ignored Icons run configurations show ignored icon to view/hide skipped tests in the Test Runner tree:

Skipped test in the Test Runner tab
Last modified: 16 June 2021