Skip to content

Idea for parameterized test macro #887

Open
@jrosiek

Description

Hi. Just a quick idea for a macro with nicer syntax and better output (IMO) than what is in the docs. Requires more modern C++ compiler due to syntax used.

#define PARAMETERIZED_SUBCASE(name, collection, variable_name)                                                         \
    DOCTEST_SUBCASE(name)                                                                                              \
      for (auto [i, it] = std::make_tuple(0, (collection).begin()); it != (collection).end(); ++it, ++i)                 \
        if (auto variable_name = *it; true)                                                                            \
          DOCTEST_SUBCASE(std::string("[" + std::to_string(i) + "] " + ::doctest::toString(variable_name).c_str()).c_str())

Usage:

auto all_values = std::vector<int>{1,2,3,4,5,6};

PARAMETERIZED_SUBCASE("my test", all_values, value)
{
  CHECK(value > 3);
}

It also has the benefit that errors are reported under specific parameter cases. That is not the case with the macro from the docs, because user checks are outside of parameter subcases.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions