Help us understand the problem. What is going on with this article?

CLionでgoogle testをいい感じに回す

More than 3 years have passed since last update.

動機

いまどきのIDEなら標準で備えているテストコード実行と結果の可視化をCLionでもやりたい

結果

できた
image
リポジトリ

蛇足

CUIでctest を動かす場合、そのままだと画面出力があっさりしすぎてテスト結果がよくわからない
(googletestの普段の出力がでなかったのでうまく動いてないと思った)

$ ctest
Test project /Users/usadamasa/workspace/cpp/sandbox
    Start 1: awesome
1/1 Test #1: awesome ..........................***Failed    0.01 sec

0% tests passed, 1 tests failed out of 1

Label Time Summary:
awesome    =   0.01 sec (1 test)
lib        =   0.01 sec (1 test)

Total Test time (real) =   0.02 sec

The following tests FAILED:
      1 - awesome (Failed)
Errors while running CTest

なので実行時には-Vオプションをつけるとよい。

$ ctest -V
UpdateCTestConfiguration  from :/Users/usadamasa/workspace/cpp/sandbox/DartConfiguration.tcl
UpdateCTestConfiguration  from :/Users/usadamasa/workspace/cpp/sandbox/DartConfiguration.tcl
Test project /Users/usadamasa/workspace/cpp/sandbox
Constructing a list of tests
Done constructing a list of tests
Checking test dependency graph...
Checking test dependency graph end
test 1
    Start 1: awesome

1: Test command: /Users/usadamasa/workspace/cpp/sandbox/bin/Debug/awesome-test
1: Test timeout computed to be: 9.99988e+06
1: [==========] Running 2 tests from 1 test case.
1: [----------] Global test environment set-up.
1: [----------] 2 tests from SqrtTest
1: [ RUN      ] SqrtTest._25_to_5
1: [       OK ] SqrtTest._25_to_5 (0 ms)
1: [ RUN      ] SqrtTest._100_to_10
1: /Users/usadamasa/workspace/cpp/sandbox/test/MathFunctionsTest/main.cpp:13: Failure
1: Value of: sandbox::Sqrt(100).GetSqrt()
1:   Actual: 10
1: Expected: 11.0
1: Which is: 11
1: [  FAILED  ] SqrtTest._100_to_10 (0 ms)
1: [----------] 2 tests from SqrtTest (0 ms total)
1: 
1: [----------] Global test environment tear-down
1: [==========] 2 tests from 1 test case ran. (0 ms total)
1: [  PASSED  ] 1 test.
1: [  FAILED  ] 1 test, listed below:
1: [  FAILED  ] SqrtTest._100_to_10
1: 
1:  1 FAILED TEST
1/1 Test #1: awesome ..........................***Failed    0.00 sec

0% tests passed, 1 tests failed out of 1

Label Time Summary:
awesome    =   0.00 sec (1 test)
lib        =   0.00 sec (1 test)

Total Test time (real) =   0.01 sec

The following tests FAILED:
      1 - awesome (Failed)
Errors while running CTest

蛇足の蛇足

--output-on-failure のほうが見やすい。

$ ctest --output-on-failure
Test project /Users/usadamasa/workspace/cpp/sandbox
    Start 1: awesome
1/1 Test #1: awesome ..........................***Failed    0.01 sec
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from SqrtTest
[ RUN      ] SqrtTest._25_to_5
[       OK ] SqrtTest._25_to_5 (0 ms)
[ RUN      ] SqrtTest._100_to_10
/Users/usadamasa/workspace/cpp/sandbox/test/MathFunctionsTest/main.cpp:13: Failure
Value of: sandbox::Sqrt(100).GetSqrt()
  Actual: 10
Expected: 11.0
Which is: 11
[  FAILED  ] SqrtTest._100_to_10 (0 ms)
[----------] 2 tests from SqrtTest (0 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (0 ms total)
[  PASSED  ] 1 test.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] SqrtTest._100_to_10

 1 FAILED TEST


0% tests passed, 1 tests failed out of 1

Label Time Summary:
awesome    =   0.01 sec (1 test)
lib        =   0.01 sec (1 test)

Total Test time (real) =   0.01 sec

The following tests FAILED:
      1 - awesome (Failed)
Errors while running CTest

宿題

デバッグビルドとリリースビルドでバイナリの出力先を分けたかった。
下記の設定をすることで、

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY
    ${CMAKE_SOURCE_DIR}/bin/${CMAKE_BUILD_TYPE}
)
cmake -DCMAKE_BUILD_TYPE=Debug .

などとすれば可能だが、何も指定していないと ${CMAKE_BUILD_TYPE} が空白になるため
${CMAKE_SOURCE_DIR}/bin 直下に出力されてしまい、いまいち。
デフォルト値のようなものが使えればよかったのだがうまくいかなかった…。

追記

これでいけた

cmake/cxx_build.cmake
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG
    ${CMAKE_SOURCE_DIR}/bin/debug
)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE
    ${CMAKE_SOURCE_DIR}/bin/release
)

参考文献リスト

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away