GTest's output has no colors when built with cmake+ninja and executed automatically

馋奶兔 提交于 2019-12-19 07:15:42

问题


I'm trying to configure CMake and ninja as a build system for my project. Except the app itself I have an extra executable for unit tests powered by gtest. I thought it would be nice to have them executed automatically whenever they are built. Here's how I made it:

├── build
└── source
    ├── CMakeLists.txt
    ├── main.cc
    └── ut
        ├── CMakeLists.txt
        ├── gtest
        │   ├── ...
        └── ut.cc

source/CMakeLists.txt...

cmake_minimum_required (VERSION 2.6)
project (trial)
add_subdirectory(ut)
add_executable(trial main.cc)

...and source/ut/CMakeLists.txt:

add_subdirectory(gtest)
include_directories ("gtest/include")

add_executable(ut ut.cc)
target_link_libraries(ut LINK_PUBLIC gtest_main)

add_custom_target(run_uts
                  COMMAND ut
                  DEPENDS ut
                  WORKING_DIRECTORY ${CMAKE_PROJECT_DIR}
                 )

Now when I build it, i.e.:

cd build
cmake -GNinja ../source
ninja run_uts

It works fine except that the output is colorless. When I run the ut binary by hand, i.e. build/ut/ut I get nice green and red colors. The colors are also there when I use Unix Makefiles as a genrator for CMake.

Since I'm only learning CMake, is there something I missed or is it an issue with Ninja?


回答1:


I assume your automated code runs a gtest executable and directs the output to a file. By default, gtest adds color sequences only when sending output to a terminal. In order to force it to add color sequences to output sent to a file or a pipe, run your test executable with the --gtest_color=yes option.



来源:https://stackoverflow.com/questions/28888998/gtests-output-has-no-colors-when-built-with-cmakeninja-and-executed-automatica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!