{"id":1797,"date":"2021-04-22T21:29:46","date_gmt":"2021-04-23T02:29:46","guid":{"rendered":"http:\/\/www.jaimerios.com\/?p=1797"},"modified":"2025-12-02T23:30:21","modified_gmt":"2025-12-02T23:30:21","slug":"unit-tests-and-cmake","status":"publish","type":"post","link":"https:\/\/jaimerios.com\/?p=1797","title":{"rendered":"Unit Tests and CMake"},"content":{"rendered":"\n<p>Here&#8217;s an example <code>CMakeLists.txt<\/code>, where I set up a unit test target, using the GTest framework:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 0 16px;font-size:0.8em;width:100%;text-align:left;background-color:#1E1E1E;font-style:italic;color:#D4D4D4\"><span style=\"border-bottom:1px solid rgba(234, 191, 191, 0.2)\">CMake<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>cmake_minimum_required(VERSION 3.19.0)\nproject(shorten_url VERSION 0.1.0)\n\n# Not necessary for this example, but I use C11 and C++20\nset(CMAKE_C_STANDARD 11)\nset(CMAKE_CXX_STANDARD 20)\n\n# Test section\n# This command attempts to find an installed copy of GoogleTest\nfind_package(GTest CONFIG REQUIRED)\n\n# This will now include GoogleTest into the project\ninclude(GoogleTest)\n\n# Here, we create our GTest application, adding the files we need for compiling the tests\nadd_executable(url_shortener_tests source\/url_shortener.cpp source\/tests\/url_shortener_tests.cpp)\n\n# We have to add our header search folders, for the compile to work\ntarget_include_directories(url_shortener_tests PRIVATE source)\n\n# Now add the libraries we want to link to the GTest target\ntarget_link_libraries(url_shortener_tests PRIVATE GTest::gmock GTest::gtest GTest::gmock_main GTest::gtest_main)\n\n# Some GTest specfics\ngtest_add_tests(TARGET      url_shortener_tests\n                TEST_SUFFIX .noArgs\n                TEST_LIST   noArgsTests\n)<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">cmake_minimum_required<\/span><span style=\"color: #D4D4D4\">(VERSION 3.19.0)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">project<\/span><span style=\"color: #D4D4D4\">(shorten_url VERSION 0.1.0)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Not necessary for this example, but I use C11 and C++20<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">set<\/span><span style=\"color: #D4D4D4\">(CMAKE_C_STANDARD 11)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">set<\/span><span style=\"color: #D4D4D4\">(CMAKE_CXX_STANDARD 20)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Test section<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># This command attempts to find an installed copy of GoogleTest<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">find_package<\/span><span style=\"color: #D4D4D4\">(GTest CONFIG REQUIRED)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># This will now include GoogleTest into the project<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">include<\/span><span style=\"color: #D4D4D4\">(GoogleTest)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Here, we create our GTest application, adding the files we need for compiling the tests<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">add_executable<\/span><span style=\"color: #D4D4D4\">(url_shortener_tests source\/url_shortener.cpp source\/tests\/url_shortener_tests.cpp)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># We have to add our header search folders, for the compile to work<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">target_include_directories<\/span><span style=\"color: #D4D4D4\">(url_shortener_tests PRIVATE source)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Now add the libraries we want to link to the GTest target<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">target_link_libraries<\/span><span style=\"color: #D4D4D4\">(url_shortener_tests PRIVATE GTest::gmock GTest::gtest GTest::gmock_main GTest::gtest_main)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\"># Some GTest specfics<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">gtest_add_tests(<\/span><span style=\"color: #569CD6\">TARGET<\/span><span style=\"color: #D4D4D4\">      url_shortener_tests<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                TEST_SUFFIX .noArgs<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                TEST_LIST   noArgsTests<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>The above should get you going with unit testing, as long as CMake can find the GTest frameworks on your computer.<\/p>\n\n\n\n<p>If you need to point CMake to another directory for the GoogleTest, GTest, stuff, you can add the following to your CMakeLists.txt file:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 0 16px;font-size:0.8em;width:100%;text-align:left;background-color:#1E1E1E;font-style:italic;color:#D4D4D4\"><span style=\"border-bottom:1px solid rgba(234, 191, 191, 0.2)\">CMake<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>set (GTEST_ROOT ${CMAKE_SOURCE_DIR}\/ExternalLibs\/gTest)<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">set<\/span><span style=\"color: #D4D4D4\"> (GTEST_ROOT <\/span><span style=\"color: #569CD6\">${CMAKE_SOURCE_DIR}<\/span><span style=\"color: #D4D4D4\">\/ExternalLibs\/gTest)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>The <code>set<\/code> command, followed by <code>GTEST_ROOT<\/code> will set that variable to the path you need. The above path is just an example and you can change it to anything.<\/p>\n\n\n\n<p>Ref:<br>\nhttps:\/\/stackoverflow.com\/questions\/8507723\/how-to-start-working-with-gtest-and-cmake<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s an example CMakeLists.txt, where I set up a unit test target, using the GTest framework: The above should get you going with unit testing, as long as CMake can find the GTest frameworks on your computer. If you need to point CMake to another directory for the GoogleTest, GTest, stuff, you can add the &#8230; <a title=\"Unit Tests and CMake\" class=\"read-more\" href=\"https:\/\/jaimerios.com\/?p=1797\" aria-label=\"Read more about Unit Tests and CMake\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[218],"class_list":["post-1797","post","type-post","status-publish","format-standard","hentry","category-coding","tag-google-c-testing"],"_links":{"self":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts\/1797","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1797"}],"version-history":[{"count":1,"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts\/1797\/revisions"}],"predecessor-version":[{"id":1883,"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts\/1797\/revisions\/1883"}],"wp:attachment":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}