CLion에서 google test를 좋은 느낌으로 돌립니다.

동기



지금의 IDE라면 표준으로 갖추고 있는 테스트 코드 실행과 결과의 시각화를 CLion에서도 하고 싶다

결과



완성

리포지토리

뱀발



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
)

참고 문헌 목록


  • Google Test support in CLion - YouTube
  • C++ 단위 테스트를 좋게 느끼기 - Qiita
  • CMake 자습서 — Cmake-Tutorial
  • CMake를 사용해 보았습니다 (2) 좀 더 괜찮은 프로젝트
  • CMake: CTest - Qiita
  • 좋은 웹페이지 즐겨찾기