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

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

結果
---
できた
![image](https://qiita-image-store.s3.amazonaws.com/0/2625/0d9f160a-d0ba-e1bb-77ce-f3b49fa56960.png)
[リポジトリ](https://github.com/usadamasa/cmaketest)

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

``` bash
$ 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`オプションをつけるとよい。

``` bash
$ 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` のほうが見やすい。

``` bash
$ 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
```

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

``` cmake
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY
    ${CMAKE_SOURCE_DIR}/bin/${CMAKE_BUILD_TYPE}
)
```

``` bash
cmake -DCMAKE_BUILD_TYPE=Debug .
```

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

## 追記
これでいけた

``` cmake: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
)
```

参考文献リスト
---
* [Google Test support in CLion \- YouTube](https://www.youtube.com/watch?v=8Up5eNZ0FLw)
* [C\+\+ のユニットテストをいい感じにする \- Qiita](http://qiita.com/janus_wel/items/4e6c12f9104f501104c7)
* [CMake チュートリアル — Cmake\-Tutorial](http://opencv.jp/cmake/cmake_tutorial.html)
* [CMakeを使ってみた（2）もう少しまともなプロジェクト \- wagavulin's blog](http://wagavulin.hatenablog.com/entry/2011/11/27/222642)
* [CMake: CTest \- Qiita](http://qiita.com/mrk_21/items/25ee7f00cebb9934b472)