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
Clone the GoogleTest repository on your computer.
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 ../Compile and install Google Test:
make sudo make install
Add Google Test to your project
The Google Test framework can be added from Xcode:
Open your project in Xcode. You can do it from AppCode by selecting
from the main menu.Select the project in the project navigator, then select the target and open the Build Phases tab.
In the Link binary with libraries section, select the generated libgtest.a file from .
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 Alt+Insert.
When called from a fixture, this menu additionally includes SetUp Method and TearDown 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 button in the gutter and select Run '<TestName>'.
Place the caret at the test declaration and press Shift+F10.
Run a test fixture
Do one of the following:
Click the button in the gutter and select Run '<TestFixtureName>'.
Place the caret at the fixture declaration and press Shift+F10.
Run tests with a run/debug configuration
Create a run/debug configuration for Google Test.
Select a created configuration in the toolbar and click or press Shift+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.
To create a Google Test configuration, go to Run | Edit Configurations, click and select Google Test from the list of templates.
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:
Set wildcards to specify test patterns, for example:
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:The output will look as follows:
Repeating all tests (iteration 1) ... Repeating all tests (iteration 2) ... Repeating all tests (iteration 3) ...Save the configuration, and it's ready for Run or Debug.
Exploring results
You can see the results of the executed tests on a separate tab of the Run tool window:
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 or on the toolbar, respectively:
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.
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:
Use the Show ignored icon to view/hide skipped tests in the Test Runner tree: