snitch-org / snitch

Lightweight C++20 testing framework.
Boost Software License 1.0
268 stars 10 forks source link

BDD-style macros #72

Open zhihaoy opened 1 year ago

zhihaoy commented 1 year ago

They are not on the roadmap, but they are important. https://github.com/doctest/doctest/blob/master/doc/markdown/testcases.md#bdd-style-test-cases The tests written in BDD are a lot more readable. https://github.com/zhihaoy/nontype_functional/blob/main/tests/function_ref/test_return_reference.cpp

cschreib commented 1 year ago

Based on what I read in the Catch2 and doctest documentations, you can easily do this with sections:

TEST_CASE("BDD") {
    SECTION("given: a vector") {
        std::vector<int> v;

        SECTION("when: resized") {
            v.resize(5u);

            SECTION("then: size changed") {
                CHECK(v.size() == 4u); // oops, wrong value for testing
            }
        }
    }
}

And if you want the extra convenience of dedicated macros:

#define SCENARIO(NAME, ...) TEST_CASE("scenario: " NAME, __VA_ARGS__)
#define GIVEN(WHAT) SECTION("given: " WHAT)
#define WHEN(WHAT) SECTION("when: " WHAT)
#define AND_WHEN(WHAT) SECTION("and when: " WHAT)
#define THEN(WHAT) SECTION("then: " WHAT)
#define AND_THEN(WHAT) SECTION("and then: " WHAT)

SCENARIO("BDD") {
    GIVEN("a vector") {
        std::vector<int> v;

        WHEN("resized") {
            v.resize(5u);

            THEN("size changed") {
                CHECK(v.size() == 4u); // oops, wrong value for testing
            }
        }
    }
}

Screenshot from 2023-03-13 08-30-51

Although the dedicated macros look nice, they may provide a false sense of security. There is no check done in case you forget to write a GIVEN(), a WHEN(), or a THEN(), or if they are specified in the wrong order. Both Catch2 and doctest have the same issue. It might be possible to implement such checks at compile-time (run-time would be easy), but this would be a fair bit of work. I won't feel comfortable adding them to snitch until such checks are in place.

There's also some more more minor issues:

Given that the exact same behavior can be obtained with plain sections, at the expense of typing a few extra characters, I'm not sure this is worth it. My recommendation would be to stick to simple sections, as in the first example, and arrange them in a BDD-style if you like.