Language: CPP
Testing
Trompeloeil was created by Björn Fahller to bring a clean, type-safe, and powerful mocking library to modern C++. Its name comes from the French term 'trompe-l'œil', meaning 'deceive the eye', reflecting the way mocks pretend to be real objects during testing. Today, Trompeloeil is used widely in C++ projects alongside testing frameworks like Catch2, doctest, and Google Test.
Trompeloeil is a modern C++14 mocking framework for unit testing. It provides a type-safe, expressive API for creating mocks, setting expectations, and verifying interactions between objects.
find_package(Trompeloeil CONFIG REQUIRED)vcpkg install trompeloeilconan install trompeloeilDownload and include trompeloeil.hpp (header-only)Trompeloeil allows developers to create mocks of interfaces or abstract classes, define expectations on method calls, and verify those expectations at runtime.
#include <trompeloeil.hpp>
#include <catch2/catch_test_macros.hpp>
struct Printer {
virtual void print(const std::string& msg) = 0;
virtual ~Printer() = default;
};
struct MockPrinter : Printer {
MAKE_MOCK1(print, void(const std::string&), override);
};
TEST_CASE("Printer prints a message") {
MockPrinter printer;
REQUIRE_CALL(printer, print("Hello"));
printer.print("Hello");
}Defines a mock `Printer` and verifies that `print("Hello")` was called once.
REQUIRE_CALL(printer, print("A"));
REQUIRE_CALL(printer, print("B"));
printer.print("A");
printer.print("B");Verifies that multiple calls happen in the expected way.
ALLOW_CALL(printer, print(trompeloeil::_)).TIMES(2);
printer.print("Hi");
printer.print("Bye");Allows a method to be called exactly two times with any string.
struct Reader {
virtual int read() = 0;
};
struct MockReader : Reader {
MAKE_MOCK0(read, int(), override);
};
MockReader reader;
REQUIRE_CALL(reader, read()).RETURN(42);
CHECK(reader.read() == 42);Mocks a method that returns a value and specifies the return.
trompeloeil::sequence seq;
REQUIRE_CALL(printer, print("First")) .IN_SEQUENCE(seq);
REQUIRE_CALL(printer, print("Second")) .IN_SEQUENCE(seq);
printer.print("First");
printer.print("Second");Ensures methods are called in a specific order.
Use Trompeloeil with a test framework like Catch2 or Google Test for assertions.
Keep mocks small and focused on the interaction under test.
Use `ALLOW_CALL` for setup, and `REQUIRE_CALL` to enforce critical expectations.
Leverage `.TIMES()`, `.RETURN()`, and `.IN_SEQUENCE()` to fine-tune behavior.
Prefer mocking interfaces or abstract base classes rather than concrete implementations.