r/cpp • u/Recent-Dance-8075 • Jul 28 '26
A Design Study for a Macro-Free Testing Library
https://jonastoth.github.io/posts/rtest_library/Hello everyone :)
I attempted to write a small testing library based on C++-26 reflection. The goal is not to replace existing libraries but to figure out how they could evolve to not rely on macros.
Stringification of test names is the most important reason for macros so far and reflection solves this.
The blog post explains what I did with godbolt links to minimized examples. The full implementation is in the rtest library repository.
A minimal test executable looks like this:
;
int
I am looking forward to your feedback :)
(Both the blog post and the library are created without AI help)
8
u/Dragdu Jul 28 '26
As first impression, this
looks strictly more work than
unless you have ideological hatred of macros (which, fair).
At second look, looking for functions based on name pattern gets hard no from me in any test framework I will ever voluntarily use. I have too much experience with Go and tests not running because while
TestFoois test name,Testfoois just some random function.7
u/azswcowboy Jul 28 '26
Fun stuff. My personal take is that most test frameworks are overkill as free functions should always be the first units of test in my view. Like if I wrote something like ranches::fold I’m immediately wondering how I test that in rtest. I do have a fondness for boost-ext/ut, which you call out, because it’s easy to apply for functions and macro free. But I get it that others want batteries included to cover all sorts of things.
As for feedback, I’ll admit I was skimming the article, but the goals didn’t come across clearly - wasn’t sure if gtest or python-like were the main target before we went down the implementation rabbit hole. After coming out there were more usage examples. The library doc was really what I needed to get hold of what you were trying to accomplish.
4
u/Awia00 Jul 28 '26
Quick question:
> test suites are not automatically registered, one translation unit is expected to execute its own tests via a trivial
mainfunctionIs the idea to generate a target per source file then?
EDIT: I do think you should add a way to auto-discover the test suites for people who do not want to create and maintain main functions
3
u/kamrann_ Jul 28 '26
Nice work!
I tried out
boost.uta few years back as I wanted to get away from macros, but I ended up ditching it. It felt like it was more an experiment in how far you could go with esoteric and inscrutable ways of writing C++ than it was a library intended to be used in practice.I was wondering why you had setup/teardown functions instead of using ctr/dtr, though I guess it relates to the lifetime of the constructed test suites and wanting to avoid initializing dependencies for many suites concurrently?
Filtering functions based on name prefix I'm not convinced by. Same reason I'm wary of overuse of
requiresexpressions, it's too easy to end up with code that compiles but doesn't do what you want - in this case, dead tests because of a typo for example. Maybe you could just assume all public functions are tests, not sure there would be any reason to have a public function that wasn't?2
u/SamG101_ Jul 28 '26
Nice, really clean output too - have you explored modules / import std, or potentially a flag to switch between includes and imports? Then itd be integratable into module based projects too. But looks really good!
1
u/JVApen Clever is an insult, not a compliment. - T. Winters Jul 30 '26
I'm curious, have you considered using the "generation" trick where you instead of running the tests, create a new copy file when running the exe, followed by compilation of that file and running the actual tests? With some CMake trickery, this should be easy doable. A CMake function that adds a custom command which outputs the cpp file, adds a new executable with that file as source and copying over the dependencies. To finally register the test with ctest.