| 
 | 1 | +# Guide to Creating a Test Suite for a New Feature in WAMR  | 
 | 2 | + | 
 | 3 | +This guide provides instructions for contributors on how to create a test suite for a new feature in the WAMR project. Follow these steps to ensure consistency and maintainability across the test framework.  | 
 | 4 | + | 
 | 5 | +---  | 
 | 6 | + | 
 | 7 | +## General Guidelines  | 
 | 8 | + | 
 | 9 | +- **Create a New Directory**:  | 
 | 10 | +  Always create a dedicated directory for a new feature under the `tests/unit/` directory.  | 
 | 11 | + | 
 | 12 | +  - Reuse existing test cases and patch them when possible to avoid redundancy.  | 
 | 13 | +  - Name the directory in lowercase with words separated by hyphens (e.g., `new-feature`).  | 
 | 14 | + | 
 | 15 | +- **Avoid Committing `.wasm` Files**:  | 
 | 16 | +  Do not commit precompiled `.wasm` files. Instead:  | 
 | 17 | + | 
 | 18 | +  - Generate `.wasm` files from `.wat` or `.c` source files.  | 
 | 19 | +  - Use `ExternalProject` and the `wasi-sdk` toolchain to compile `.wasm` files during the build process.  | 
 | 20 | + | 
 | 21 | +- **Keep Using `ctest` as the framework**:  | 
 | 22 | +  Continue to use `ctest` for running the test cases, as it is already integrated into the existing test framework.  | 
 | 23 | + | 
 | 24 | +---  | 
 | 25 | + | 
 | 26 | +## Writing `CMakeLists.txt` for the Test Suite  | 
 | 27 | + | 
 | 28 | +When creating a `CMakeLists.txt` file for your test suite, follow these best practices:  | 
 | 29 | + | 
 | 30 | +1. **Do Not Fetch Googletest Again**:  | 
 | 31 | +   The root `unit/CMakeLists.txt` already fetches Googletest. Avoid including or fetching it again in your test suite.  | 
 | 32 | + | 
 | 33 | +2. **Find LLVM on Demand**:  | 
 | 34 | +   If your test suite requires LLVM, use `find_package` to locate LLVM components as needed. Do not include LLVM globally unless required.  | 
 | 35 | + | 
 | 36 | +3. **Include `unit_common.cmake`**:  | 
 | 37 | +   Always include `../unit_common.cmake` in your `CMakeLists.txt` to avoid duplicating common configurations and utilities.  | 
 | 38 | + | 
 | 39 | +   Example:  | 
 | 40 | + | 
 | 41 | +   ```cmake  | 
 | 42 | +   include("../unit_common.cmake")  | 
 | 43 | +   ```  | 
 | 44 | + | 
 | 45 | +4. **Use `WAMR_RUNTIME_LIB_SOURCE`**:  | 
 | 46 | +   Replace long lists of runtime source files with the `WAMR_RUNTIME_LIB_SOURCE` variable to simplify your configuration.  | 
 | 47 | + | 
 | 48 | +   Example:  | 
 | 49 | + | 
 | 50 | +   ```cmake  | 
 | 51 | +   target_sources(your_test_target PRIVATE ${WAMR_RUNTIME_LIB_SOURCE})  | 
 | 52 | +   ```  | 
 | 53 | + | 
 | 54 | +5. **Avoid Global Compilation Flags**:  | 
 | 55 | +   Do not define global compilation flags in the `unit` directory. Each test case should specify its own compilation flags based on its unique requirements.  | 
 | 56 | + | 
 | 57 | +---  | 
 | 58 | + | 
 | 59 | +## Generating `.wasm` Files  | 
 | 60 | + | 
 | 61 | +- **Compile `.wasm` Files Dynamically**:  | 
 | 62 | +  Use `ExternalProject` in your `CMakeLists.txt` to compile `.wasm` files from `.wat` or `.c` source files.  | 
 | 63 | +  - Use the `wasi-sdk` toolchain for `.c` or `.cc` source files.  | 
 | 64 | +  - Example configuration:  | 
 | 65 | +    ```cmake  | 
 | 66 | +    ExternalProject_Add(  | 
 | 67 | +        generate_wasm  | 
 | 68 | +        SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps  | 
 | 69 | +        BUILD_ALWAYS YES  | 
 | 70 | +        CONFIGURE_COMMAND  ${CMAKE_COMMAND} -S ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps -B build  | 
 | 71 | +                              -DWASI_SDK_PREFIX=${WASI_SDK_DIR}  | 
 | 72 | +                              -DCMAKE_TOOLCHAIN_FILE=${WASISDK_TOOLCHAIN}  | 
 | 73 | +        BUILD_COMMAND      ${CMAKE_COMMAND} --build build  | 
 | 74 | +        INSTALL_COMMAND    ${CMAKE_COMMAND} --install build --prefix ${CMAKE_CURRENT_BINARY_DIR}/wasm-apps  | 
 | 75 | +    )  | 
 | 76 | +    ```  | 
 | 77 | +- **Example for `wasm-apps` Directory**:  | 
 | 78 | +  Place your source files in a `wasm-apps/` subdirectory within your test suite directory.  | 
 | 79 | +
  | 
 | 80 | +  - Create a `CMakeLists.txt` in `wasm-apps/` to handle the compilation of these files.  | 
 | 81 | +  - Example `CMakeLists.txt` for `wasm-apps/`:  | 
 | 82 | +
  | 
 | 83 | +    ```cmake  | 
 | 84 | +    cmake_minimum_required(VERSION 3.13)  | 
 | 85 | +    project(wasm_apps)  | 
 | 86 | +
  | 
 | 87 | +    add_executable(example example.c)  | 
 | 88 | +    set_target_properties(example PROPERTIES SUFFIX .wasm)  | 
 | 89 | +    install(TARGETS example DESTINATION .)  | 
 | 90 | +    ```  | 
 | 91 | +
  | 
 | 92 | +---  | 
 | 93 | +
  | 
 | 94 | +## Compiling and Running Test Cases  | 
 | 95 | +
  | 
 | 96 | +To compile and run the test cases, follow these steps:  | 
 | 97 | +
  | 
 | 98 | +1. **Generate Build Files**:  | 
 | 99 | +
  | 
 | 100 | +   ```bash  | 
 | 101 | +   cmake -S . -B build  | 
 | 102 | +   ```  | 
 | 103 | + | 
 | 104 | +2. **Build the Test Suite**:  | 
 | 105 | + | 
 | 106 | +   ```bash  | 
 | 107 | +   cmake --build build  | 
 | 108 | +   ```  | 
 | 109 | + | 
 | 110 | +3. **Run the Tests**:  | 
 | 111 | + | 
 | 112 | +   ```bash  | 
 | 113 | +   ctest --test-dir build --output-on-failure  | 
 | 114 | +   ```  | 
 | 115 | + | 
 | 116 | +   This will compile and execute all test cases in the test suite, displaying detailed output for any failures.  | 
 | 117 | + | 
 | 118 | +4. **List all Tests**:  | 
 | 119 | +   To see all available test cases, use:  | 
 | 120 | + | 
 | 121 | +   ```bash  | 
 | 122 | +   ctest --test-dir build -N  | 
 | 123 | +   ```  | 
 | 124 | + | 
 | 125 | +5. **Run a Specific Test**:  | 
 | 126 | +   To run a specific test case, use:  | 
 | 127 | +   ```bash  | 
 | 128 | +   ctest --test-dir build -R <test_name> --output-on-failure  | 
 | 129 | +   ```  | 
 | 130 | + | 
 | 131 | +---  | 
 | 132 | + | 
 | 133 | +## Collecting Code Coverage Data  | 
 | 134 | + | 
 | 135 | +To collect code coverage data using `lcov`, follow these steps:  | 
 | 136 | + | 
 | 137 | +1. **Build with Coverage Flags**:  | 
 | 138 | +   Ensure the test suite is built with coverage flags enabled:  | 
 | 139 | + | 
 | 140 | +   ```bash  | 
 | 141 | +   cmake -S . -B build -DCOLLECT_CODE_COVERAGE=1  | 
 | 142 | +   cmake --build build  | 
 | 143 | +   ```  | 
 | 144 | + | 
 | 145 | +2. **Run the Tests**:  | 
 | 146 | +   Execute the test cases as described above.  | 
 | 147 | + | 
 | 148 | +3. **Generate Coverage Report**:  | 
 | 149 | +   Use `lcov` to collect and generate the coverage report:  | 
 | 150 | + | 
 | 151 | +   ```bash  | 
 | 152 | +   lcov --capture --directory build --output-file coverage.all.info  | 
 | 153 | +   lcov --extract coverage.all.info "*/core/iwasm/*" "*/core/shared/*" --output-file coverage.info  | 
 | 154 | +   genhtml coverage.info --output-directory coverage-report  | 
 | 155 | +   ```  | 
 | 156 | + | 
 | 157 | +4. **View the Report**:  | 
 | 158 | +   Open the `index.html` file in the `coverage-report` directory to view the coverage results in your browser.  | 
 | 159 | + | 
 | 160 | +5. **Summary of Coverage**:  | 
 | 161 | +   To get a summary of the coverage data, use:  | 
 | 162 | + | 
 | 163 | +   ```bash  | 
 | 164 | +   lcov --summary coverage.info  | 
 | 165 | +   ```  | 
 | 166 | + | 
 | 167 | +---  | 
 | 168 | + | 
 | 169 | +## Example Directory Structure  | 
 | 170 | + | 
 | 171 | +Here’s an example of how your test suite directory might look:  | 
 | 172 | + | 
 | 173 | +```  | 
 | 174 | +new-feature/  | 
 | 175 | +├── CMakeLists.txt  | 
 | 176 | +├── new_feature_test.cc  | 
 | 177 | +├── wasm-apps/  | 
 | 178 | +|   ├── CMakeLists.txt  | 
 | 179 | +│   ├── example.c  | 
 | 180 | +│   └── example.wat  | 
 | 181 | +```  | 
 | 182 | + | 
 | 183 | +---  | 
 | 184 | + | 
 | 185 | +## Additional Notes  | 
 | 186 | + | 
 | 187 | +- **Testing Framework**: Use Googletest for writing unit tests. Refer to existing test cases in the `tests/unit/` directory for examples.  | 
 | 188 | +- **Documentation**: Add comments in your test code to explain the purpose of each test case.  | 
 | 189 | +- **Edge Cases**: Ensure your test suite covers edge cases and potential failure scenarios.  | 
 | 190 | +- **Reuse Utilities**: Leverage existing utilities in `common/` (e.g., `mock_allocator.h`, `test_helper.h`) to simplify your test code.  | 
 | 191 | + | 
 | 192 | +---  | 
 | 193 | + | 
 | 194 | +By following these guidelines, you can create a well-structured and maintainable test suite that integrates seamlessly with the WAMR testing framework.  | 
0 commit comments