15

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?

MyDeveloperDay
  • 2,485
  • 1
  • 17
  • 20
  • 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](http://stackoverflow.com/questions/28888998/gtests-output-has-no-colors-when-built-with-cmakeninja-and-executed-automatica)? Can you give the googletest option `--gtest_color=yes` a try? – Florian Dec 13 '15 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 – MyDeveloperDay Dec 18 '15 at 13:18
  • 6
    "export GTEST_COLOR=1" worked for me (on Ubuntu). – Étienne Oct 17 '16 at 11:19
  • @Étienne It worked for me, thx! – Antonio Petricca Dec 02 '16 at 06:04

3 Answers3

16

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

export GTEST_COLOR=1
Étienne
  • 4,773
  • 2
  • 33
  • 58
  • 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 '22 at 04:51
12

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
MaEtUgR
  • 353
  • 3
  • 8
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

Simon Puente
  • 451
  • 1
  • 8
  • 21