Unit test frameworks

I’ve never understood why people use GTest, CppUnit, Catch, oh wait, Catch2 now.
Here is my C++ unit test framework:

// test object constructor calls function
struct test {
    test(const std::function& f)
    {
        try {
            f();
        }
        catch (const std::exception& ex) {
            fputs(ex.what(), stderr);
        }
    }
};

Just write a int main() { return 0; } file and add .cpp files that call your tests.
They look something like this:

test my_function([]()
{
    assert (my_function_works());
});

Just make sure my_function_works throws an exception with an informative message. I use ensure for that.
More examples can be found here.
Why make things complicated?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s