No description
Find a file
2025-12-03 19:54:30 +01:00
.vscode Add float conversion compiler check and fix 2025-11-19 21:36:47 +01:00
cmake fix(cmake): Drop cmake < 3.13 compatibility in AddCMockaTest.cmake 2025-11-27 10:39:01 +01:00
coverity coverity: Add missing functions to model file 2023-02-03 12:47:42 +01:00
cppcheck feat(cppcheck): Add a README.md 2025-11-20 08:10:53 +01:00
doc fix(docs): The cmocka_version.h documentation was missing 2025-11-25 15:01:09 +01:00
example docs(example): Add documentation for assert_module*.c 2025-11-24 18:26:33 +01:00
include fix(xml): Store the XML output path in a global variable 2025-12-03 19:54:30 +01:00
src fix(message_output): We need to do sanity checking 2025-12-03 19:54:30 +01:00
tests tests(output): Add tests to check CMOCKA_MESSAGE_OUTPUT 2025-12-03 19:54:30 +01:00
.clang-format clang-format: Update to reflect coding style 2024-02-06 11:07:06 +01:00
.clang_complete Add obj directory for config.h to .clang_complete. 2013-12-13 10:55:40 +01:00
.editorconfig Update editorconfig 2022-02-26 20:42:14 +01:00
.gitignore gitignore: Add .cache directory 2022-03-02 09:04:01 +01:00
.gitlab-ci.yml fix(cmake): Set CMAKE_CROSSCOMPILING_EMULATOR 2025-11-20 21:52:39 +01:00
.ycm_extra_conf.py Add YouCompleteMe config. 2014-04-11 15:09:12 +02:00
AUTHORS cmocka: Implement a new test runner. 2015-02-08 10:29:09 +01:00
CHANGELOG.md chore(changelog): Update for latest changes 2025-11-27 15:30:51 +01:00
CMakeLists.txt chore(cmake): Set minimum required version to 3.13 2025-11-26 11:46:19 +01:00
cmocka-build-tree-settings.cmake.in cmake: Fix usage of cmocka with build tree 2019-10-22 09:09:06 +02:00
cmocka-config.cmake.in cmake: Set CMOCKA_LIBRARIES in package config for backwards compatibility 2023-03-22 15:38:32 +01:00
cmocka.pc.cmake cmake: Fix path relocation in pkgconfig file for mingw 2022-12-01 19:16:08 +01:00
CompilerChecks.cmake Add float conversion compiler check and fix 2025-11-19 21:36:47 +01:00
config.h.cmake Don't redefine uintptr_t 2019-11-19 15:41:58 +01:00
ConfigureChecks.cmake treewide: Use bool intead of int wherever possible 2024-02-12 15:57:02 +01:00
CONTRIBUTING.md Add initial CONTRIBUTING.md 2024-02-12 15:56:37 +01:00
CPackConfig.cmake Rename COPYING to LICENSE 2025-02-01 10:11:15 +01:00
CTestConfig.cmake ctest: Move to new dashboard 2018-07-05 10:18:59 +02:00
DefineOptions.cmake cmocka: Remove deprecated functions and cmockery legacy support 2020-10-12 08:25:26 +02:00
LICENSE Rename COPYING to LICENSE 2025-02-01 10:11:15 +01:00
meson.build meson.build: install library if not a subproject 2023-08-01 11:05:44 +02:00
meson_options.txt meson: Build and run unit tests 2019-08-22 11:41:49 +02:00
README.md doc(README): Add some more details 2025-11-21 14:02:58 +01:00
sbom.cdx.json Add a SBOM template in CycloneDX format 2025-02-05 09:34:25 +01:00

cmocka

cmocka is an elegant unit testing framework for C with support for mock objects. It only requires the standard C library, works on a range of computing platforms (including embedded) and with different compilers.

Features

  • Support for mock objects
  • Test fixtures (setup and teardown functions)
  • Only requires a C library
  • Exception handling for signals (SIGSEGV, SIGILL, ...)
  • No fork()
  • Very well tested
  • Testing of memory leaks, buffer overflows and underflows
  • A set of assert macros
  • Several supported output formats (stdout, TAP, JUnit XML, Subunit)
  • License: Apache License 2.0

Supported Platforms

cmocka has been tested on:

  • Linux (various distributions)
  • BSD (FreeBSD, OpenBSD, NetBSD)
  • Solaris
  • macOS
  • Windows

Supported Compilers

  • GCC
  • Clang
  • Microsoft Visual Studio
  • MinGW

Quick Start

Simple Test Example

#include <cmocka.h>

/* A test that will always pass */
static void null_test_success(void **state) {
    (void) state; /* unused */
}

/* A test that will always fail */
static void null_test_fail(void **state) {
    (void) state; /* unused */
    assert_true(0);
}

int main(void) {
    const struct CMUnitTest tests[] = {
        cmocka_unit_test(null_test_success),
        cmocka_unit_test(null_test_fail),
    };
    return cmocka_run_group_tests(tests, NULL, NULL);
}

Building the Example

gcc -o example example.c -lcmocka
./example

Building cmocka

Requirements

  • CMake >= 3.10.0
  • C compiler (GCC, Clang, MSVC, etc.)

Building on Linux/Unix

# Configure the build (creates build directory)
cmake -S . -B build

# Build the library
cmake --build build

# Run tests
cmake --build build --target test

# Install (optional)
sudo cmake --install build

Building on Windows

The easiest way to use cmocka on Windows is with vcpkg:

vcpkg install cmocka

Alternatively, you can build from source using the CMake GUI or command line:

cmake -S . -B build -G "Visual Studio 16 2019"
cmake --build build

Testing

To run the cmocka test suite:

cmake -S . -B build -DUNIT_TESTING=ON
cmake --build build
cmake --build build --target test

For verbose test output:

cd build
ctest -V

Documentation

To build the documentation locally:

cmake --build build --target docs

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for detailed guidelines.

Quick overview:

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add or update unit tests!
  5. Ensure all tests pass
  6. Submit a merge request

For bug reports and feature requests, please use the issue tracker at: https://gitlab.com/cmocka/cmocka/

License

cmocka is licensed under the Apache License 2.0. See the COPYING file for details.

Resources