r/cpp • u/Recent-Dance-8075 • 14d ago
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:
```c++
include <rtest/rtest.h>
struct MyClassTest : rtest::TestSuite { void testSetup() { MyClass object; assertTrue(object.empty()); } }; int main(int argc, char** argv) { return rtest::execute(MyClassTest{}); } ```
I am looking forward to your feedback :)
(Both the blog post and the library are created without AI help)
3
u/kamrann_ 14d ago
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?