16

I am building and running unit tests built with googletest inside a cmake project with ctest enabled

I run the tests with "ctest -VV"

but the test output does not color the "red" and "green"

[ RUN ] [ OK ] [ PASSSED ]

Does anyone know if there is an options to ctest to allow those colors to bleed through to the console?

4
  • 2
    Without knowing the way you added googletest to CMake nor your host environment, it's just a guess: Could this be duplicate to GTest's output has no colors when built with cmake+ninja and executed automatically? Can you give the googletest option --gtest_color=yes a try?
    – Florian
    Dec 13, 2015 at 21:00
  • 2
    So I tried --gtest_color=yes and that didn't work... but some more googling led me to GTEST_COLOR=1, setting this in my .travis.yml made the tests appear colored... but on my command line it looks like this 1: ←[0;32m[ RUN ] ←[mStringTest.Case, this suggest that googletest might be incorrectly thinking I'm an xterm, when actually I'm running bash inside a windows command prompt Dec 18, 2015 at 13:18
  • 6
    "export GTEST_COLOR=1" worked for me (on Ubuntu).
    – Étienne
    Oct 17, 2016 at 11:19
  • @Étienne It worked for me, thx! Dec 2, 2016 at 6:04

3 Answers 3

16

As the OP suggested, I added this line to my .bashrc and it worked:

export GTEST_COLOR=1
1
  • sadly, this doesn't work within the Windows Developer Command Prompt. GTEST_COLOR only seems to help when calling the Google Test runner manually; trying to use ctest still gives uncolored output :(
    – phetdam
    May 28, 2022 at 4:51
13

Maybe you don't want to export any variable to global scope and only have colors in one ctest call. In that case use this single command:

GTEST_COLOR=1 ctest -V
7

In cmake you can pass environment variables like that:

add_executable(testExecutable
        my_test.cpp)

target_link_libraries(testExecutable
        gtest)

add_test(NAME testExecutable
        COMMAND testExecutable)

add_custom_target(check
        COMMAND env CTEST_OUTPUT_ON_FAILURE=1 GTEST_COLOR=1 ${CMAKE_CTEST_COMMAND}
        DEPENDS testExecutable)

run $ make check

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.