Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BDD-style macros #72

Open
zhihaoy opened this issue Mar 10, 2023 · 1 comment
Open

BDD-style macros #72

zhihaoy opened this issue Mar 10, 2023 · 1 comment
Labels
enhancement New feature or request maybe For issues that aren't critial, or need more exploration

Comments

@zhihaoy
Copy link

zhihaoy commented Mar 10, 2023

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
Copy link
Member

cschreib commented Mar 13, 2023

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:

  • SCENARIO() would need separate macros to support template test case, etc., which means more macros to maintain.
  • GIVEN, WHEN, and THEN are fairly common words; I would be a bit wary of adding them to the global namespace. The prefixed version (SNITCH_GIVEN(), etc.) would be far less readable, which defeats the purpose.

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.

@cschreib cschreib added enhancement New feature or request maybe For issues that aren't critial, or need more exploration labels Mar 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request maybe For issues that aren't critial, or need more exploration
Projects
None yet
Development

No branches or pull requests

2 participants