{"id":13339589,"url":"https://github.com/pthom/cmake_registertest","last_synced_at":"2025-04-12T22:34:22.994Z","repository":{"id":81535022,"uuid":"91177311","full_name":"pthom/cmake_registertest","owner":"pthom","description":"cmake scripts for googletest / catch / doctest.  Automatic tests registration, even inside library code.","archived":false,"fork":false,"pushed_at":"2018-10-30T21:17:53.000Z","size":137,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-10T19:45:59.599Z","etag":null,"topics":["catch","cmake","cmake-scripts","cpp","doctest","googletest","static-library","unit-testing","unit-tests"],"latest_commit_sha":null,"homepage":"","language":"CMake","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pthom.png","metadata":{"files":{"readme":"README.MD","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-13T13:46:44.000Z","updated_at":"2022-06-01T08:33:28.000Z","dependencies_parsed_at":null,"dependency_job_id":"80f1076d-05bd-4767-bbc1-c4d15489f230","html_url":"https://github.com/pthom/cmake_registertest","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pthom%2Fcmake_registertest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pthom%2Fcmake_registertest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pthom%2Fcmake_registertest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pthom%2Fcmake_registertest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pthom","download_url":"https://codeload.github.com/pthom/cmake_registertest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248642308,"owners_count":21138350,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["catch","cmake","cmake-scripts","cpp","doctest","googletest","static-library","unit-testing","unit-tests"],"created_at":"2024-07-29T19:20:22.060Z","updated_at":"2025-04-12T22:34:22.603Z","avatar_url":"https://github.com/pthom.png","language":"CMake","readme":"# Cmake Register Test for libraries (crt)\n\nThis is a set of cmake utilites in order to make it easy to add unit tests to a C++ library, when using googletest, catch or doctest.\n\nIt provides a cmake function `crt_registertest()` with the following features :\n\n* Automatic discovery of the tests (*outside* of even *inside* the library source files)\n* Automatic creation of an executable target for the tests, and registration with `ctest` (i.e `make test`)\n* Automatic link the test target with the test library (providing an approriate main() function)\n* Automatic inclusion of the test library include path\n\n----\n## Motto : \nTests code should be able to reside inside separate source files, *as well as directly inside the library source files*\n\nThe [doctest](https://github.com/onqtam/doctest) documentation says rightfully:\n\n\u003e Tests can be considered a form of documentation and should be able to reside near the production code which they test.\n\nHowever, storing the test code is inside a library is often cumbersome : your tests might not be launched at all! The linker often strips the self-registration code when it is inside a library. \n\nThis project provides a solution that enables to have tests both inside the library sources, as well as inside separate source files.\n\nFor more information about the possible issues that this project attempts to solve, see:\n* [Catch : \"issue 421 - Document How to make Catch tests in a library be found\"](https://github.com/philsquared/Catch/issues/421) \n* [Doctest: issue 21 - Tests inside a static library](https://github.com/onqtam/doctest/issues/21)\n* [Googletest primer : important-note-for-visual-c-users](https://github.com/google/googletest/blob/master/googletest/docs/Primer.md#important-note-for-visual-c-users\u003e)\n\n\n\n----\n\n\n## Platforms\nThis project was tested under OSX, Windows and Linux\n\n## Requirements\n* [googletest](https://github.com/google/googletest), [catch](https://github.com/philsquared/Catch), or [doctest](https://github.com/onqtam/doctest)\n* cmake\n\n## Quick usage instructions\n\n`src/crt_registerstaticlibrary.cmake` provides a `crt_registertest()` functions that make it possible to add tests to a library, using a simple instruction in the `CMakeList.txt` of your library.\n\nTwo cases are possible:\n\n### Case 1 : your test code resides in separate files\n\n```cmake\nadd_library(MyLibrary STATIC lib1.cpp)\ncrt_registertest(\n  LIBRARY MyLibrary\n  OUTPUT_TEST_TARGET MyLibraryTest\n  TEST_SOURCES lib1_test.cpp\n)\n```\n\nThis will :\n1. Create an executable test target (MyLibraryTest) for your library.\n2. Register it as a cmake test (so that `ctest` or `make test`) will launch it.\n3. Link this test target with your library\n4. Append the test library include path to your `TEST_SOURCES`\n\nFor an example, see [example_doctest_outertests](examples/example_doctest_outertests):\n* [Main CMakeLists.txt](examples/example_doctest_outertests/CMakeLists.txt)\n* [Library CMakeLists.txt](examples/example_doctest_outertests/MyLibrary/CMakeLists.txt)\n\n\n### Case 2 : your test code resides in the library source code (as well as separate files)\n\n```cmake\nadd_library(MyLibrary OBJECT lib1.cpp lib2.cpp)\ncrt_registertest(\n  TEST_INPLACE  \n  INPUT_OBJECT_LIBRARY MyLibrary\n  OUTPUT_LIBRARY_NAME MyLibrary_Static\n  OUTPUT_LIBRARY_TYPE STATIC\n  OUTPUT_TEST_TARGET MyLibraryTest\n  TEST_SOURCES lib3_test.cpp lib4_test.cpp \n)\n```\n\nThis will :\n1. First compile your library source files (lib1.cpp and lib2.cpp) as an \"object\" library. \n2. Create a static or dynamic library from these files (see `OUTPUT_LIBRARY_NAME` and `OUTPUT_LIBRARY_TYPE`). Your source files will be compiled only once.\n3. Create an executable test target (MyLibraryTest) for your library.\n4. Register it as a cmake test (so that `ctest` or `make test`) will launch it.\n5. Append the test library include path to your `TEST_SOURCES`, as well as to the library sources\n6. Make sure that all tests are run (whether they are inside the library source files or inside `TEST_SOURCES`)\n\nFor an example, see [example_doctest](examples/example_doctest):\n* [Main CMakeLists.txt](examples/example_doctest/CMakeLists.txt)\n* [Library CMakeLists.txt](examples/example_doctest/MyLibrary/CMakeLists.txt)\n\n\n## Detailed steps :\n\n### Step 1 : project structure\n\nThe structure of your project should resemble the tree shown below:\n\n```\nYourProject/\n├── CMakeLists.txt\n├── YourLibrary/\n│   ├── CMakeLists.txt\n│   ├── lib1.cpp\n│   └── lib2.cpp\n├── catch                         \n│   ├── catch.hpp                 # Only one of \n├── doctest                       #  catch\n│   ├── doctest.h                 #  doctest\n├── gtest                         #  or googletest is required\n│   ├── include/gtest/gtest.h     \n|\n└── cmake_registertest/           #  copy or reference cmake_registertest as a submodule at the root of your project\n   └──   src\n          ├── cpp_runners\n          │   ├── catch_dynamic.cpp\n          │   ├── catch_main.cpp\n          │   ├── doctest_dynamic.cpp\n          │   └── doctest_main.cpp\n          ├── crt_registertest.cmake\n          ├── crt_registertest_catch.cmake\n          ├── crt_registertest_doctest.cmake\n          └── crt_registertest_googletest.cmake\n```\n\n\n### Step 2 : Modify your main CMakeList.txt file\n\n#### Typical main CMakeList.txt file content (example using doctest)\n```cmake\ncmake_minimum_required(VERSION 3.0)\nproject(my_project)\nset (crt_test_lib_location ${CMAKE_SOURCE_DIR}/doctest)\ninclude(\"${CMAKE_SOURCE_DIR}/cmake_registertest/src/crt_registertest_doctest.cmake\")\nenable_testing()\nadd_subdirectory(MyLibrary)\n```\n\n1. Inside your main `CMakeLists.txt`, set the path to the include path of your test library (catch, doctest or googletest)\nDepending upon your test library, include one of the 3 lines below:\n```cmake\nset (rsl_test_lib_location ${CMAKE_SOURCE_DIR}/catch)\nset (rsl_test_lib_location ${CMAKE_SOURCE_DIR}/doctest)\nset (rsl_test_lib_location ${CMAKE_SOURCE_DIR}/gtest/include)\n```\n2. Include the script in your main CMakeLists.txt file\n*After* having set `rsl_test_lib_location` include the correct script.Depending upon your test library, include one of the 3 lines below:\n```cmake\ninclude(\"${CMAKE_SOURCE_DIR}/cmake_registertest/src/crt_registertest_catch.cmake\")\ninclude(\"${CMAKE_SOURCE_DIR}/cmake_registertest/src/crt_registertest_googletest.cmake\")\ninclude(\"${CMAKE_SOURCE_DIR}/cmake_registertest/src/crt_registertest_doctest.cmake\")\n```\n3. enable tests for your project\n```cmake\nenable_testing()\n```\n\n### Step 3 : Register tests for your library\n\nin the `CMakeLists.txt` of your library, call `crt_registertest()`\n\n\n## More info\n\nSee the [examples](examples)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpthom%2Fcmake_registertest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpthom%2Fcmake_registertest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpthom%2Fcmake_registertest/lists"}