{"id":21951634,"url":"https://github.com/toxe/catch2-examples","last_synced_at":"2025-10-14T17:29:43.423Z","repository":{"id":134564197,"uuid":"409846240","full_name":"Toxe/catch2-examples","owner":"Toxe","description":"Examples for structuring CMake projects using Catch2","archived":false,"fork":false,"pushed_at":"2021-11-13T08:29:13.000Z","size":163,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-10T06:33:07.035Z","etag":null,"topics":["catch2","cmake","cpp","ctest","example"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Toxe.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2021-09-24T05:44:53.000Z","updated_at":"2025-01-17T14:42:53.000Z","dependencies_parsed_at":"2023-06-18T19:09:54.337Z","dependency_job_id":null,"html_url":"https://github.com/Toxe/catch2-examples","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Toxe/catch2-examples","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Toxe%2Fcatch2-examples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Toxe%2Fcatch2-examples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Toxe%2Fcatch2-examples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Toxe%2Fcatch2-examples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Toxe","download_url":"https://codeload.github.com/Toxe/catch2-examples/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Toxe%2Fcatch2-examples/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279020076,"owners_count":26086806,"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","status":"online","status_checked_at":"2025-10-14T02:00:06.444Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["catch2","cmake","cpp","ctest","example"],"created_at":"2024-11-29T06:15:46.144Z","updated_at":"2025-10-14T17:29:43.413Z","avatar_url":"https://github.com/Toxe.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Catch2 Examples\n\nA couple of examples for structuring CMake projects using [Catch2](https://github.com/catchorg/Catch2),\neither as a single header or as a statically compiled library (Catch2 v3). Compatible with CTest.\n\n\u003e Note: All of these are highly contrived examples. The code we are testing sits in a small library (`src/libtestee`) and usually the test files would/should be inside the same directory that contains the code itself. But for the purpose of these examples the test files are actually outside of that directory tree.\n\n- [Build](#build)\n- [`src/libtestee`](#srclibtestee)\n- [Single Header Examples](#single-header-examples)\n- [Catch2 v3 Examples](#catch2-v3-examples)\n- [CTest](#ctest)\n- [Build Performance Comparison](#build-performance-comparison)\n\n## Build\n\nDefault build instructions for CMake.\n\n#### Linux + Mac\n\n```\n$ mkdir build\n$ cd build\n$ cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug ..\n$ ninja\n```\n\n#### Windows\n\n```\n$ mkdir build\n$ cd build\n$ cmake -DCMAKE_BUILD_TYPE=Debug ..\n$ cmake --build . --config Debug\n```\n\n#### Disable Tests\n\nTests are enabled by default. To disable building tests set the CMake flag `BUILD_TESTING` to `OFF`:\n\n```\n$ cmake [..] -DBUILD_TESTING=OFF\n```\n\n#### Sanitizers and Static Analysis\n\nThe code can be compiled with sanitizer and static analysis support. Look here for the details: https://github.com/Toxe/cpp-sanitizers.\nThe test binaries themselves won't be build with sanitizer support.\n\n#### Disable Building Catch2 v2 or v3 Examples\n\nTo prevent building Catch2 v2 or v3 examples set the CMake flags `DISABLE_CATCH2v2` or `DISABLE_CATCH2v3` to `ON`:\n\n```\n$ cmake [..] -DDISABLE_CATCH2v2=ON\n```\n\n## `src/libtestee`\n\nA small library with some simple functions for testing. Just so that we don't repeat the code that we want to test in every example.\n\n\u003e Note: Usually the test files that test these functions would/should be inside this directory. But for the purpose of these examples the test files are outside of that directory tree.\n\n## Single Header Examples\n\nThese example projects all include their own single header version of Catch2 v2.13.7.\n\n### `src` `/` `single_header` `/` `multiple_test_files`\n\n- Uses and includes the single header version of Catch2 v2.13.7.\n- Multiple test files, one for each source file in `libtestee`.\n\n```\n$ ./build/src/single_header/multiple_test_files/multiple_test_files\nEverything seems to be working.\n\n$ ./build/src/single_header/multiple_test_files/single_header_test_add\n===============================================================================\nAll tests passed (5 assertions in 1 test case)\n\n$ ./build/src/single_header/multiple_test_files/single_header_test_mul\n===============================================================================\nAll tests passed (5 assertions in 1 test case)\n\n$ ./build/src/single_header/multiple_test_files/single_header_test_sub\n===============================================================================\nAll tests passed (6 assertions in 1 test case)\n```\n\n### `src` `/` `single_header` `/` `single_test_file_one_source`\n\n- Uses and includes the single header version of Catch2 v2.13.7.\n- One big test file, containing multiple sections.\n\n```\n$ ./build/src/single_header/single_test_file_one_source/single_test_file_one_source\nEverything seems to be working.\n\n$ ./build/src/single_header/single_test_file_one_source/tests\n===============================================================================\nAll tests passed (16 assertions in 1 test case)\n```\n\n### `src` `/` `single_header` `/` `single_test_file_multiple_sources`\n\n- Uses and includes the single header version of Catch2 v2.13.7.\n- One test file, combining multiple sources into one binary.\n\n```\n$ ./build/src/single_header/single_test_file_multiple_sources/single_test_file_multiple_sources\nEverything seems to be working.\n\n$ ./build/src/single_header/single_test_file_multiple_sources/tests\n===============================================================================\nAll tests passed (16 assertions in 3 test cases)\n```\n\n## Catch2 v3 Examples\n\nThese example projects use the newest version of Catch2 v3 (`devel` branch) as a static library, which greatly increases compile time.\nIt is downloaded automatically from GitHub via CMake [`FetchContent`](https://cmake.org/cmake/help/latest/module/FetchContent.html).\n\n### `src` `/` `catch2v3` `/` `multiple_test_files`\n\n- Catch2 v3 static library.\n- Multiple test files, one for each source file in `libtestee`.\n\n```\n$ ./build/src/catch2v3/multiple_test_files/multiple_test_files\nEverything seems to be working.\n\n$ ./build/src/catch2v3/multiple_test_files/catch2v3_test_add\n===============================================================================\nAll tests passed (5 assertions in 1 test case)\n\n$ ./build/src/catch2v3/multiple_test_files/catch2v3_test_mul\n===============================================================================\nAll tests passed (5 assertions in 1 test case)\n\n$ ./build/src/catch2v3/multiple_test_files/catch2v3_test_sub\n===============================================================================\nAll tests passed (6 assertions in 1 test case)\n```\n\n### `src` `/` `catch2v3` `/` `single_test_file_one_source`\n\n- Catch2 v3 static library.\n- One big test file, containing multiple sections.\n\n```\n$ ./build/src/catch2v3/single_test_file_one_source/single_test_file_one_source\nEverything seems to be working.\n\n$ ./build/src/catch2v3/single_test_file_one_source/tests\n===============================================================================\nAll tests passed (16 assertions in 1 test case)\n```\n\n### `src` `/` `catch2v3` `/` `single_test_file_multiple_sources`\n\n- Catch2 v3 static library.\n- One test file, combining multiple sources into one binary.\n\n```\n$ ./build/src/catch2v3/single_test_file_multiple_sources/single_test_file_multiple_sources\nEverything seems to be working.\n\n$ ./build/src/catch2v3/single_test_file_multiple_sources/tests\n===============================================================================\nAll tests passed (16 assertions in 3 test cases)\n```\n\n## CTest\n\nEither run each test file individually (see above) or with CTest.\n\n```\n$ cd build\n\n$ ctest -N\nTest project /home/toxe/Programmierung/catch2-examples/build\n  Test  #1: single_header_test_add\n  Test  #2: single_header_test_mul\n  Test  #3: single_header_test_sub\n  Test  #4: single_header_single_test_file_multiple_sources_tests\n  Test  #5: single_header_single_test_file_one_source_tests\n  Test  #6: catch2v3_test_add\n  Test  #7: catch2v3_test_mul\n  Test  #8: catch2v3_test_sub\n  Test  #9: catch2v3_single_test_file_multiple_sources_tests\n  Test #10: catch2v3_single_test_file_one_source_tests\n\nTotal Tests: 10\n\n$ ctest\nTest project /home/toxe/Programmierung/catch2-examples/build\n      Start  1: single_header_test_add\n 1/10 Test  #1: single_header_test_add ..................................   Passed    0.01 sec\n      Start  2: single_header_test_mul\n 2/10 Test  #2: single_header_test_mul ..................................   Passed    0.01 sec\n      Start  3: single_header_test_sub\n 3/10 Test  #3: single_header_test_sub ..................................   Passed    0.01 sec\n      Start  4: single_header_single_test_file_multiple_sources_tests\n 4/10 Test  #4: single_header_single_test_file_multiple_sources_tests ...   Passed    0.01 sec\n      Start  5: single_header_single_test_file_one_source_tests\n 5/10 Test  #5: single_header_single_test_file_one_source_tests .........   Passed    0.01 sec\n      Start  6: catch2v3_test_add\n 6/10 Test  #6: catch2v3_test_add .......................................   Passed    0.01 sec\n      Start  7: catch2v3_test_mul\n 7/10 Test  #7: catch2v3_test_mul .......................................   Passed    0.01 sec\n      Start  8: catch2v3_test_sub\n 8/10 Test  #8: catch2v3_test_sub .......................................   Passed    0.01 sec\n      Start  9: catch2v3_single_test_file_multiple_sources_tests\n 9/10 Test  #9: catch2v3_single_test_file_multiple_sources_tests ........   Passed    0.01 sec\n      Start 10: catch2v3_single_test_file_one_source_tests\n10/10 Test #10: catch2v3_single_test_file_one_source_tests ..............   Passed    0.01 sec\n\n100% tests passed, 0 tests failed out of 10\n\nTotal Test time (real) =   0.09 sec\n```\n\nTo run the tests in parallel:\n\n```\n$ ctest --progress -j8\nTest project /home/toxe/Programmierung/catch2-examples/build\n10/10 Test  #6: catch2v3_test_add\n100% tests passed, 0 tests failed out of 10\n\nTotal Test time (real) =   0.02 sec\n```\n\n## Build Performance Comparison\n\nBuild environment:\n\n- AMD Ryzen 9 3900X (12 cores)\n- Ubuntu 21.04 running on WSL2 on Windows 10\n- Clang 12 with LibC++\n- Debug build\n\nTimes are measured either recompiling the whole project (including `libtestee`, the main programs, all the tests and in case of Catch2 v3 the static library)\nor just rebuilding the test programs after their code has changed.\n\n#### Catch2 v2 Single Header\n\nBuilding only Catch2 v2 examples.\n\n|               | ninja -j 1 | ninja -j 8 | ninja -j 26 |\n| ------------- | ---------- | ---------- | ----------- |\n| clean rebuild | 32.862 s   | 7.350 s    | 7.196 s     |\n| only tests    | 30.857 s   | 7.094 s    | 6.816 s     |\n\n#### Catch2 v3 Static Library\n\nBuilding only Catch2 v3 examples.\n\n|               | ninja -j 1 | ninja -j 8 | ninja -j 26 |\n| ------------- | ---------- | ---------- | ----------- |\n| clean rebuild | 43.332 s   | 6.839 s    | 3.703 s     |\n| only tests    | 5.741 s    | 1.109 s    | 1.022 s     |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoxe%2Fcatch2-examples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftoxe%2Fcatch2-examples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoxe%2Fcatch2-examples/lists"}