Overview
Qt-Mocks is a modern C++17 mocking and testing framework inspired by Google Test, designed specifically to work seamlessly with the Qt Test framework. It provides expressive, type-safe testing capabilities that make unit tests more readable and maintainable. The library focuses on three core priorities, in order of importance:
- Simplicity - Code should be clear and self-documenting in its purpose
- Efficiency - Fast execution for rapid testing cycles
- Type Safety - Compile-time type checking to catch errors early
Qt-Mocks bridges the gap between Qt's testing framework and modern C++ mocking capabilities, offering a familiar API for developers experienced with Google Test whilst maintaining full Qt integration.
Features
Qt-Mocks provides a comprehensive set of testing capabilities:
Mock Object Creation
- Virtual function mocking - Create mock implementations of interfaces using the
QMOCK macro
- Custom behaviour binding - Associate custom functions with mocked methods using
QMOCK_BIND
- Support for complex signatures - Handle const, noexcept, and other function specifiers
- Up to 10 parameters - Mock functions with multiple arguments
Expectation Management
- Behaviour specification - Define what mock functions should return with
.will(give(value))
- Call verification - Ensure mocked methods are called as expected with
.require()
- Expectations object - Centralised management of mock expectations
Matchers and Assertions
- Enhanced assertions - Use
QVERIFY_THAT for more descriptive test assertions
- Value matching - Built-in matchers like
equals() for clear comparisons
- Container support - Test equality for Qt and STL container types
- Custom matchers - Extensible matcher system for domain-specific testing
Output and Debugging
- Stream insertion operators - Automatic output formatting for Qt containers (lists, maps, etc.)
- Raw byte fallback - Display raw object bytes when stream operators are unavailable
- Clear error messages - Informative failure messages to speed up debugging
Features
Library Structure
The library is organised into several main parts:
Core Components
Mock.hpp - Core mocking infrastructure and QMOCK/QMOCK_BIND macros for creating mock objects
Expectation.hpp - Expectation management system for specifying and verifying mock behaviour
Verify.hpp - Enhanced assertion macros like QVERIFY_THAT for expressive test validation
Matchers.hpp - Matcher library for flexible value comparisons and validations
Supporting Infrastructure
Output.hpp - Stream operators for Qt types and custom output formatting
Information.hpp - Library version and build information utilities
Building and Installation
See the README for detailed build and installation instructions.
The library uses CMake as its build system and supports:
- CMake 3.20 or higher
- C++17 standard
- Qt 6
- Multiple platforms: Linux (Debian, Fedora, Mageia), Windows
- Both static and shared library builds
License
Licensed under the GNU Lesser General Public Licence v3.0 or later.