New CLion 1.2 EAP (build 143.249.8) brings you Google Test

Getting closer to the 1.2 release, today we are happy to introduce Google Test support in CLion! It’s taken quite a lot of work, and though some issues are still present we are ready to share the results with all of you and anxious to hear your feedback.

Download the build from our confluence page, or get a patch update if you’re using a previous EAP build.

Google Test support consists of three features: unit tests configuration, code generation in tests, and a test runner. We’ll talk about each one in detail, but before we start, let us share a couple of tips on how to add Google Test to your project (feel free to skip this part if you already have it added).

Start with Google Test
Since installing the pre-compiled version of Google Test is not recommended, you will probably be compiling Google Test inside your project. To start, download Google Test and include it into your project. After creating one or several directories for the tests, your project structure will look something like this:
gt_project
Sample CMakeLists.txt in your test directories will look similar to this:
gt_cmake_file

That’s mostly it! However, take note of several details regarding some specific platforms:

  • In case you are using Cygwin, use the GNU dialect of the C++ language for your project:
  • On OS X, only in case you select Google Test v1.6.0, you’ll need to add the following lines to your project’s CMakeLists.txt:

Unit tests configuration
You can run Google Test as a regular executable, but to benefit from CLion’s built-in test runner you should create a special Google Test configuration. There are several ways of doing this:

  • Place the cursor on any test and run it from the context menu or by pressing Ctrl+Shift+F10 on Windows/Linux, Ctrl+Shift+R on OS X. The corresponding configuration will be created automatically, and you can rerun it later:
    one_test_config
  • The same works for running all tests from one particular file or related to one fixture. Select a file or place a cursor inside it, or place a cursor on a fixture’s name and use context menu or Ctrl+Shift+F10 on Windows/Linux, Ctrl+Shift+R on OS X shortcut. Again, the configuration will be created and saved automatically for you:
    all_class_config
  • Go to Run | Edit Configurations… and add a new Google Test configuration from the existing template:
    GoogleTest_config
    Then select a proper target, suite and test name, or set a pattern to run several tests.

While running one test, a set of several tests or a complete fixture or file, CLion will open a separate tool window. See what you can benefit from in the next section.

Test runner
Test runner window includes the following elements:

  • The progress bar shows the percentage of tests executed so far. The color also indicates whether the tests have passed successfully or not.
  • The pane with a tree view of all the tests running, including the information about test status (via icon) and test duration.
  • If you select a test in the test pane, the output pane will show the output of that particular test, including all the messages sent to the output stream, and the error messages.

The default layout of the testing results tool window is horizontal; if you prefer vertical, move the window to the right or left, and the console will be adjusted accordingly (note that currently you need to rerun the tests to get proper adjustments):
test_runner

Test runner window also allows you to:

  • rerun any test in particular;
  • rerun all failed tests;
  • sort tests alphabetically or by duration;
  • jump to the source code of a particular test with F4 on Windows/Linux, Cmd+Down on OS X;
  • export test results; and
  • open previous results from the history, saved automatically.

Code generation
CLion’s code generation feature (Alt+Insert on Windows/Linux, Cmd+N on OS X) can be a lot of help in writing tests. Use it to generate test, test fixture or SetUp/TearDown Methods:
code_generation
Generate Test, specify a fixture name and one of the appropriate TEST_F(), TEST_P(), TYPED_TEST(), TYPED_TEST_P() will be used by CLion automatically:
gt_gen

In addition, we’d like to add that if you are new to Google Test, check out the very useful FAQ at Google’s wiki. Get our sample project with Google Tests from github.

Give this EAP build a try and let us know what you think in the comments below! Please submit any possible problems to our tracker and see complete release notes.

Develop with pleasure,
The CLion Team

This entry was posted in Announcement, Early Access Preview and tagged , , . Bookmark the permalink.

33 Responses to New CLion 1.2 EAP (build 143.249.8) brings you Google Test

  1. Balázs Kocsi says:

    Including gtest into a cmake project can be done automatically:
    http://crascit.com/2015/07/25/cmake-gtest/

    It will download, build and include in your project. CMake magic :)

  2. Yury V. Zaytsev says:

    Let’s hope that the parser is going to stop blowing up randomly on crazy Google Test macros, still used to happen quite frequently the week before last :-/ The UI looks great though!

    • Yury V. Zaytsev says:

      Oh, re-indexing is much faster on git stash / git stash pop, that’s great news, it was really driving me crazy! I guess I was affected by the platform bug referenced in the release notes.

    • Anastasia Kazakova says:

      Feel free to report cases if they are not already in the tracker. This will help a lot!

  3. Joshua Rosen says:

    Is it possible to use the built-in test runner GUI while using a testing framework other than Google Test (in my case, a custom framework)? If not, are there plans to eventually support this? Admittedly, I’m not exactly sure how to generalize an API for the test runner…

  4. Nick says:

    Its only been 10 minutes but this release is frick’n awesome, can’t wait for 1.2!

  5. Jonas says:

    Is it possible to get the demo project for getting started with GoogleTest? Thank you.

  6. Andrew Luca says:

    Thanks for making world better! :)
    I also started to use Google Test within CLion, but i’m using it without having googletest added to my project. First I installed googletest on my system, just made a clean build, then I just used CMake commands

    project(project_tests)
    find_package(GTEST REQUIRED)
    add_executable(BasicTests WorldTest.cpp)
    target_link_libraries(BasicTests ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES})
    target_link_libraries(BasicTests SFECS)

    Everything works like a charm! :)
    Good day folks!

  7. sircodesalot says:

    Ohhh my god! Been waiting for GTest support forever, and now its here!

  8. Alexey Klimkin says:

    How do I run multiple test executables together with the built-in test-runner?
    Here is an example of a build file:
    http://stackoverflow.com/questions/3065220/ctest-with-multiple-commands

    • Anastasia Kazakova says:

      Unfortunately, CLion currently doesn’t support CTest (https://youtrack.jetbrains.com/issue/CPP-905), so can’t provide you any solution now. CLion doesn’t detect test scripts presence so the approach from SO doesn’t work for now in CLion. Hope to have this added in future. Pls upvote and follow the updates in tracker.

  9. John Gluszak says:

    If anyone’s using a mac, I turned this into a Ruby gem to cut down on project setup time. It takes one line in Terminal to create a project running a sample unit test.
    https://github.com/JohnGluszak/cmaker-ruby-gem

  10. Albert says:

    It awesome!!! Thank you!

  11. David K Lind says:

    I’m running CLion 2017.1.1.

    Is there a way, from CMake, to automatically add projects to the Google Test list in Run/Debug Configurations?

    • Anastasia Kazakova says:

      Yes, CLion automatically created Google Test configurations for the targets linked with gtest or gmock (2017.2). You just need to reload CMake in CLion and that’s it.

  12. Marc C says:

    Hello,

    Would it be possible to point me towards to latest and greatest way to install and run gtest.
    Information out there seems to point in different direction since things have evolved since 2015.

    Thank you.

    • Anastasia Kazakova says:

      Things haven’t changed that much and general way of getting gtest into your project is still as described. So feel free to download gtest and build it into your project as suggested in the blog post.

  13. Josiah says:

    I am having issues setting this up:

    cmake_minimum_required(VERSION 3.9)
    project(Eight_Queen_Solver_v3)

    set(CMAKE_CXX_STANDARD 17)

    add_executable(Eight_Queen_Solver_v3 main.cpp core/Node.cpp core/Node.h core/Problem.cpp core/Problem.h core/InformedSearch.cpp core/InformedSearch.h)

    # information for gtests
    set(CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -std=gnu++0x”)
    include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})

    target_link_libraries(Eight_Queen_Solver_v3 gtest gtest_main)
    target_link_libraries(Eight_Queen_Solver_v3 core exceptions)

    But I am getting:
    [100%] Linking CXX executable Eight_Queen_Solver_v3.exe
    /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lgtest
    /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lgtest_main
    /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lcore
    /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lexceptions
    collect2: error: ld returned 1 exit status
    make[2]: *** [CMakeFiles/Eight_Queen_Solver_v3.dir/build.make:173: Eight_Queen_Solver_v3.exe] Error 1
    make[1]: *** [CMakeFiles/Makefile2:68: CMakeFiles/Eight_Queen_Solver_v3.dir/all] Error 2
    make: *** [Makefile:84: all] Error 2

    • Anastasia Kazakova says:

      And where is the Google Test library located?

      • Josiah says:

        Eight_Queen_Solver_v3 -> lib -> gtest
        Eight_Queen_Solver_v3 -> core -> Node.cpp

        I wish I could just upload a picture of my directory. I copied everything that they did, but still cant make tests.

      • Josiah says:

        The post says: To start, download Google Test and include it into your project. After creating one or several directories for the tests, your project structure will look something like this

        But how do I include it? How is gtest_SOURCE_DIR initialized???? This isn’t explained.

Leave a Reply to Josiah Cancel reply

Your email address will not be published. Required fields are marked *