GLib Mock is a framework for mocking non-GObject C functions, aiming to be added to the GLib Testing framework as the standardized way to create mocks.
-
Intercept standard functions or system calls and redirect them to mock implementations. This allows you to monitor data flow, alter behavior, or simulate various kernel responses without modifying the original source code. Mocks can either replace the target functionality entirely or act as a "shim" that wraps and calls the real implementation. For an example on how this works, please refer to wrap-function test.
-
If you are familiar with unit testing in any programming language, you will know that some projects may have dependencies that require user interaction, complex syscalls, or external hardware states. This framework allows you to provide predictable, scripted responses to these dependencies.
-
Target shared objects and DLLs require no modifications or recompilation. The framework performs runtime IAT patching on Windows, and on Linux/macOS it takes advantage of the dynamic loader (
ld.so,dyld) and its dynamic loading order. -
Provides a unified API that handles the technical differences between all platforms transparently.
-
Includes built-in validation to ensure every registered mock is successfully applied across the process, preventing silent test failures.
glib-mock can be integrated as a Meson subproject or installed system-wide.
-
-
Create
subprojects/glib-mock.wrap:[wrap-git] url = https://gitlab.gnome.org/otaxhu/glib-mock.git revision = main depth = 1 [provide] dependency_names = glib-mock
-
-
-
Run the following commands to build and install globally:
meson setup _build ninja -C _build ninja -C _build install # You may need root privileges
-
After you succesfully installed the library, you can run tests by doing the following:
-
Add to your
meson.build:libglib_mock_dep = dependency('glib-mock')
-
Then in your meson test:
test_executable = executable( ..., dependencies: [libglib_mock_dep], ) test( 'your-test', test_executable, )
-
glib-mockcan only intercept functions that are dynamically linked (shared libraries) and dynamically loaded (so no LTO inlines). It will not work with libraries linked statically.
| Feature | Linux | macOS | Windows |
|---|---|---|---|
| Interposition method | Native interposition by exporting symbol at compilation time (user just writes a regular exported function) | Same as in Linux | IAT / GetProcAddress runtime patching |
| Mock load-time imports | ✅ | ✅ | |
| Mock run-time imports | ✅ (via dlsym) |
✅ (via dlsym) |
✅ (via GetProcAddress, or dlsym if using Cygwin) |
-
Windows: Run-time dynamically linked functions
The framework supports mocking run-time dynamically linked functions on Windows. This ensures that even if a library is loaded at runtime via
LoadLibrary(), any request for a symbol will transparently return your mock implementation. -
macOS: Requires flat namespace
This is a requirement for the framework in order to perform interposition without doing any recompilation of libraries (other methods require recompilation and modification to sources in order to perform interpostion). Ultimately, this was the less invasive method to implement the framework in macOS, and may be unstable due to the use of flat-namespaces.