ã¯ããã«
CMakeã«ã¯CTestã¨ãããã¹ãã©ã³ãã¼ãããã
CTestã¯ãã¹ãç¨ã®å®è¡ãã¡ã¤ã«(ãã¹ããã¤ããª)ã1ã¤ã®ãã¹ãã¨ãã¦èªèãããã
GoogleTest(GTest)ã®ãããªãã¹ããã¬ã¼ã ã¯ã¼ã¯ã®å ´åã1ã¤ã®ãã¹ããã¤ããªã«è¤æ°ã®ãã¹ãã±ã¼ã¹ãå«ã¾ãããã¨ã常ã¨ãªãã
ããã§CMakeã«ã¯GTestã®ãã¹ããä¸ã¤ã®CTestã¨ãã¦æ±ãããããã«ããããã®ãµãã¼ãæ©è½ãããã
ãã¹ã対象ã®ããã°ã©ã
CMake C++ã§ã¦ããããã¹ãå ¥é(åç´ç·¨)ã§ä½æããããªãã®å½¹ã«ãç«ããªããã¹ãããã°ã©ã ã使ç¨ããã
使ç¨ããã®ã¯ä¸è¨ã®ãã¡ã¤ã«ã
- hello.cpp
- hello.h
GTest
aptã§ã¤ã³ã¹ãã¼ã«
ExternalProject
ã¨ãããããããããã¾ããã以å¤ã®ãã¨ã試ãããã®ã§ãgoogletestã®ããã±ã¼ã¸ãaptã§ã¤ã³ã¹ãã¼ã«ããã
$ sudo apt install -y googletest
ã¡ãªã¿ã«ä½æ¥ç°å¢ã¯Ubuntu 20.04
ãã¹ã
ãã®ãµã³ãã«ããã°ã©ã ã«å¯¾ããGoogleTestã以ä¸ã®ããã«ä½ã£ã¦ã¿ãã
#include "gtest/gtest.h" #include "hello.h" #include <stdexcept> Hello h; /* This block will uncomment after. TEST(HelloTest, NullPtr) { EXPECT_THROW(h.hello(nullptr), std::runtime_error); } */ TEST(HelloTest, default_param) { EXPECT_EQ(h.hello(), "empty"); } TEST(HellsoTest, empty_string) { EXPECT_EQ(h.hello(""), "empty"); } TEST(HelloTest, normal_case) { EXPECT_EQ(h.hello("John Doe"), "Hello John Doe"); }
NullPtr
ã®ãã¹ãã¯ããã¦ã³ã¡ã³ãã¢ã¦ããã¦ããã
CMakeLists.txt
ã¾ãã¯ããã¹ãã©ã³ãã¼ã使ç¨ããã«ãGTestããªã³ã¯ãããã¹ããã¤ããªãçæããã
cmake_minimum_required(VERSION 3.10) # To use the googletest find_package(GTest REQUIRED) # Add the executable for the testcase which is using googletest add_executable(test_hello test_hello.cpp hello.cpp) target_link_libraries(test_hello GTest::GTest GTest::Main)
ãã¹ããã¤ããªã®ãã«ã
ãã®æç¹ã§ãã£ã¬ã¯ããªæ§æã¯æ¬¡ã®ããã«ãªã£ã¦ããã
. âââ CMakeLists.txt âââ hello.cpp âââ hello.h âââ test_hello.cpp
次ã®ããã«ãã¦ãã«ãããã
$ mkdir build && cd build $ cmake .. # make -j $(nproc)
ãã¹ããã¤ããªã®å®è¡
$ ./test_hello Running main() from /home/mickey/work/trash/googletest-release-1.10.0/googletest/src/gtest_main.cc [==========] Running 3 tests from 1 test suite. [----------] Global test environment set-up. [----------] 3 tests from HelloTest [ RUN ] HelloTest.default_param [ OK ] HelloTest.default_param (0 ms) [ RUN ] HelloTest.empty_string [ OK ] HelloTest.empty_string (0 ms) [ RUN ] HelloTest.normal_case [ OK ] HelloTest.normal_case (0 ms) [----------] 3 tests from HelloTest (0 ms total) [----------] Global test environment tear-down [==========] 3 tests from 1 test suite ran. (0 ms total) [ PASSED ] 3 tests.
3ã¤ã®ãã¹ããPASSãã¦ããã
ããã¾ã§ã§ä¸æºåå®äºã
gtest_add_tests
gtest_add_testsã試ãããã®æ©è½ã¯CMake 3.1ã®æç¹ã§è¿½å ããã¦ããã
ä»ã®å½¢ã«ãªã£ãã®ã¯3.9ã®é ãããã
CMakeLists.txtã®ä¿®æ£
次ã®ããã«ãã¦gtest_add_tests
ã使ç¨ãã¦ã¿ãã
cmake_minimum_required(VERSION 3.10) project(hello) # Enable the testing features. enable_testing() # To use the googletest find_package(GTest REQUIRED) # Enable the GoogleTest integration. include(GoogleTest) # Add the executable for the testcase which is using googletest add_executable(test_hello test_hello.cpp hello.cpp) target_link_libraries(test_hello GTest::GTest GTest::Main) # Add the test case use the old feature. gtest_add_tests(TARGET test_hello)
ctestã®å®è¡
ctestã§ãã¹ããå®è¡ãã¦ã¿ãã
$ ctest Test project /home/mickey/work/c_lang/gtest/build Start 1: HelloTest.NullPtr 1/4 Test #1: HelloTest.NullPtr ................ Passed 0.00 sec Start 2: HelloTest.default_param 2/4 Test #2: HelloTest.default_param .......... Passed 0.00 sec Start 3: HelloTest.empty_string 3/4 Test #3: HelloTest.empty_string ........... Passed 0.00 sec Start 4: HelloTest.normal_case 4/4 Test #4: HelloTest.normal_case ............ Passed 0.00 sec 100% tests passed, 0 tests failed out of 4 Total Test time (real) = 0.01 sec
ã³ã¡ã³ãã¢ã¦ãããã¦ããã¯ãã®NullPtr
ãã¹ããå®è¡ããã¦ããã
gtest_add_testsã®å¼±ç¹
ä¸ã¤ã®ãã¹ããã¤ããªãæå®ããã¨ãGTestã®ãã¹ãã±ã¼ã¹ã«å¯¾å¿ãã¦GTestãå®è¡ãã¦ãããããä¸è¨ã®åé¡ç¹ãã¯ããã§ããã
- ãã¹ãæ½åºã®ã¿ã¤ãã³ã°ãcmakeå®è¡æ
- æååãã¼ã¹ã§ãã¹ããæ½åºãããããã³ã¡ã³ãã¢ã¦ãããã¦ãããã¨ãåºå¥ããªã
ã¤ã¾ããCã³ã³ãã¤ã©ã¯å½ç¶ã®ããã«ç¥ã£ã¦ããã³ã¡ã³ãè¡ã#if 0
ãªã©ã¯æç¥ãããªããããTEST
ãã¯ãã®è¡ãæç´ã«æ¤åºãã¦ãã¾ãã
ã¾ãããã¹ãã®æ¤åºãcmakeå®è¡æã§ããããããã¹ãã±ã¼ã¹ã追å ãããåé¤ãããããå ´åãcmakeããå®è¡ãç´ãå¿ è¦ãããã
gtest_discover_tests
gtest_discover_testsã試ãããã®æ©è½ã¯3.10ã§è¿½å ããã¦ãã
CMakeLists.txtã®ä¿®æ£
次ã®ããã«ãã¦gtest_discover_tests
ã使ç¨ãã¦ã¿ãã
cmake_minimum_required(VERSION 3.10) project(hello) # Enable the testing features. enable_testing() # To use the googletest find_package(GTest REQUIRED) # Enable the GoogleTest integration. include(GoogleTest) # Add the executable for the testcase which is using googletest add_executable(test_hello test_hello.cpp hello.cpp) target_link_libraries(test_hello GTest::GTest GTest::Main) # Add the test case use the gtest feature. gtest_discover_tests(test_hello)
ctestã®å®è¡
ctestã§ãã¹ããå®è¡ãã¦ã¿ãã
$ ctest Test project /home/mickey/work/c_lang/gtest/build Start 1: HelloTest.default_param 1/3 Test #1: HelloTest.default_param .......... Passed 0.00 sec Start 2: HelloTest.empty_string 2/3 Test #2: HelloTest.empty_string ........... Passed 0.00 sec Start 3: HelloTest.normal_case 3/3 Test #3: HelloTest.normal_case ............ Passed 0.00 sec 100% tests passed, 0 tests failed out of 3 Total Test time (real) = 0.01 sec
ã³ã¡ã³ãã¢ã¦ãããã¦ããNullPtr
ãã¹ãã¯å®è¡ããã¦ããªãã
NullPtrã³ã¡ã³ãã®åé¤
NullPtrã®ãã¹ããã¢ã³ã³ã¡ã³ããã¦å®è¡ãã¦ã¿ãã
$ make $ ctest Test project /home/mickey/work/c_lang/gtest/build Start 1: HelloTest.NullPtr 1/4 Test #1: HelloTest.NullPtr ................ Passed 0.00 sec Start 2: HelloTest.default_param 2/4 Test #2: HelloTest.default_param .......... Passed 0.00 sec Start 3: HelloTest.empty_string 3/4 Test #3: HelloTest.empty_string ........... Passed 0.00 sec Start 4: HelloTest.normal_case 4/4 Test #4: HelloTest.normal_case ............ Passed 0.00 sec 100% tests passed, 0 tests failed out of 4 Total Test time (real) = 0.01 sec
cmakeããã«ãmakeã§ãã¹ããã¤ããªããã«ããç´ãã ãã§NullPtrã®ãã¹ããå®è¡ãããããã«ãªã£ãã
gtest_discover_testsã®å¼·ã¿
- ãã¹ãæ½åºã®ã¿ã¤ãã³ã°ããã«ãæ
- ã³ã³ãã¤ã«æã®æ å ±ããã¨ã«æ½åºãããããã³ã¡ã³ãã¢ã¦ãããããã¹ããªã©ã¯æ½åºãããªã
ã¤ã¾ããã³ã¡ã³ãã#if 0
ãªã©ã¯ã¯ããªããã»ããµãã³ã³ãã¤ã©ã®ã«ã¼ã«ã«å¾ã£ã¦é©åã«å¦çããããããæ示çã«å®è¡ããããªããã¹ãã«é¢ãã¦å®è¡ããã¦ãã¾ããã¨ããªãã
ã¾ãããã¹ãã®æ½åºããã«ãæã§ããããããã¹ãã±ã¼ã¹ã追å ãããåé¤ãããããå ´åãcmakeããå®è¡ããå¿ è¦ããªãã
ã¾ã¨ã
CMakeã«ã¯CTestã¨GoogleTestããã¾ã強調ããããã®æ©è½ãæä¾ããã¦ããã
gtest_add_tests
ã¨gtest_discover_tests
ããããã åçç¡ç¨ã§gtest_discover_testsã®æ¹ã使ãã¹ã
æ¥æ¬èªã®æ å ±ã§æ¤ç´¢ããã¨gtest_add_testsãåºã¦ããå ´åãå¤ãã®ã§æ³¨æãå¿ è¦ã