|
| 1 | +--- |
| 2 | +title: Modules & Scripts |
| 3 | +description: How to write tests for your CMake modules and scripts |
| 4 | +keywords: testing, cmake |
| 5 | +author: RSP Systems A/S |
| 6 | +--- |
| 7 | + |
| 8 | +# Testing CMake Modules & Scripts |
| 9 | + |
| 10 | +The `testing` module includes a "_mini_" framework for testing your CMake modules and scripts. It is built on top of |
| 11 | +[CTest](https://cmake.org/cmake/help/latest/manual/ctest.1.html#manual:ctest(1)). |
| 12 | + |
| 13 | +[TOC] |
| 14 | + |
| 15 | +## Getting Started |
| 16 | + |
| 17 | +The following guide illustrates how you can get started. It is by no means a comprehensive guide on how to write tests, |
| 18 | +but rather just a starting point. |
| 19 | + |
| 20 | +### Directory Structure |
| 21 | + |
| 22 | +Create an appropriate directory in which your tests should reside. For instance, this can be located in the root |
| 23 | +of your project. You can name it "tests", or whatever makes the most sense for you. The important part is to isolate |
| 24 | +tests from the remaining of your project's CMake logic. |
| 25 | + |
| 26 | +The following example is a possible directory and files structure, that you can use. |
| 27 | + |
| 28 | +``` |
| 29 | +/tests |
| 30 | + /unit |
| 31 | + /assets |
| 32 | + build_assets_test.cmake |
| 33 | + CMakeLists.txt |
| 34 | +CMakeLists.txt |
| 35 | +``` |
| 36 | + |
| 37 | +### Define Test Suite |
| 38 | + |
| 39 | +In your `/tests/CMakeLists.txt`, define the [test suite(s)](./01_test_suite.md) for your project. |
| 40 | + |
| 41 | +```cmake |
| 42 | +# ...in your /tests/CMakeLists.txt |
| 43 | +
|
| 44 | +enable_testing() |
| 45 | +
|
| 46 | +project(my_package_tests LANGUAGES NONE) |
| 47 | +
|
| 48 | +include("rsp/testing") |
| 49 | +
|
| 50 | +# Define the test suites for this project |
| 51 | +define_test_suite("unit" DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/unit") |
| 52 | +``` |
| 53 | + |
| 54 | +### Define Test Case |
| 55 | + |
| 56 | +To define a new test case, invoke the [`define_test_case()`](./02_test_case.md) function. |
| 57 | +This should be done in each test file (_e.g. in the `/tests/unit/assets/build_assets_tests.cmake` from the above |
| 58 | +shown example directory and files structure)._ |
| 59 | + |
| 60 | +```cmake |
| 61 | +# ... in your test file |
| 62 | +
|
| 63 | +include("rsp/testing") |
| 64 | +
|
| 65 | +define_test_case( |
| 66 | + "Build Assets Test" |
| 67 | + LABELS "assets;build" |
| 68 | +) |
| 69 | +
|
| 70 | +# ... remaining not shown ... |
| 71 | +``` |
| 72 | + |
| 73 | +### Define Test(s) |
| 74 | + |
| 75 | +Once your test-case has been defined, you can define the tests. To do so, you need to define the function that must |
| 76 | +be invoked when the test-case is executed. This is done by the [`define_test()`](./03_test.md) function. |
| 77 | + |
| 78 | +```cmake |
| 79 | +# ... in your test file |
| 80 | +
|
| 81 | +# ... previous not shown ... |
| 82 | +
|
| 83 | +define_test("can build assets" "can_build_assets") |
| 84 | +function(can_build_assets) |
| 85 | + |
| 86 | + # ... testing logic not shown ... |
| 87 | + |
| 88 | + assert_truthy(assets_built MESSAGE "Assets could bot be built...") |
| 89 | +endfunction() |
| 90 | +
|
| 91 | +define_test("fails if assets not ready" "fails_when_not_ready" EXPECT_FAILURE) |
| 92 | +function(fails_when_not_ready) |
| 93 | +
|
| 94 | + # ... testing logic not shown ... |
| 95 | +
|
| 96 | + assert_truthy(false MESSAGE "Assets should not be built when not ready...") |
| 97 | +endfunction() |
| 98 | +
|
| 99 | +# ... etc |
| 100 | +``` |
| 101 | + |
| 102 | +### Build & Run |
| 103 | + |
| 104 | +_TODO: ..._ |
| 105 | + |
| 106 | +## Caveats |
| 107 | + |
| 108 | +_TODO: ..._ |
0 commit comments