Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions docs/test-macros.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,41 @@ documenting known bugs that cannot yet be fixed.
CHECK_EQUAL(expected, buggy_function()); // currently produces wrong result
}

Parameterized Tests
--------------------

Write a test body once, then run it against several cases without repeating
it. Each case still becomes its own ordinary test - independently named,
filterable, and reported on failure - so a failing case never hides its
siblings.

``PARAMETERIZED_TEST(group, name, ParamsType)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:c:macro:`PARAMETERIZED_TEST` defines the shared body, taking a ``params``
value of ``ParamsType``. Write this before the
:c:macro:`PARAMETERIZED_TEST_CASE` lines that use it.

``PARAMETERIZED_TEST_CASE(group, name, id, expr)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:c:macro:`PARAMETERIZED_TEST_CASE` registers one case as
``TEST(group, name_id)``, calling the shared body with the value of
``expr``.

.. literalinclude:: ../examples/tests/ParameterizedTest.test.cpp
:language: cpp
:lines: 1-37

To reuse the same set of cases across several checks - for example, running
an interface-conformance suite against every implementation of an interface -
write a small local macro that expands to one
:c:macro:`PARAMETERIZED_TEST_CASE` per case and invoke it after each body:

.. literalinclude:: ../examples/tests/ParameterizedTest.test.cpp
:language: cpp
:lines: 39-103

Metadata
--------

Expand Down Expand Up @@ -171,6 +206,8 @@ These bridge C test files into the C++ test runner. See
- Same as above but expected to fail
* - :c:macro:`TEST_ORDERED_C_WRAPPER(group, name, level) <TEST_ORDERED_C_WRAPPER>`
- Wires a C test function into a :c:macro:`TEST_ORDERED`
* - :c:macro:`PARAMETERIZED_TEST_CASE_C_WRAPPER(group, name, id, ParamsType, expr) <PARAMETERIZED_TEST_CASE_C_WRAPPER>`
- Wires one case into a C :c:macro:`PARAMETERIZED_TEST` body

Examples
--------
Expand All @@ -187,3 +224,5 @@ Examples
- C-wrapper two-file pattern with :c:macro:`TEST_GROUP_SETUP`, :c:macro:`TEST`, and :c:macro:`TEST_C_WRAPPER`
* - :source:`OrderedTest.test.cpp <examples/tests/OrderedTest.test.cpp>`
- :c:macro:`TEST_ORDERED` alongside regular :c:macro:`TEST`
* - :source:`ParameterizedTest.test.cpp <examples/tests/ParameterizedTest.test.cpp>`
- :c:macro:`PARAMETERIZED_TEST` and :c:macro:`PARAMETERIZED_TEST_CASE`, including reusing one case list across several checks
1 change: 1 addition & 0 deletions examples/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ add_executable(ExampleTests
MockDocumentation.test.c
MockDocumentation.test.cpp
OrderedTest.test.cpp
ParameterizedTest.test.cpp
Printer.test.cpp
TeamCityOutputPlugin.cpp
TeamCityTestOutput.test.cpp
Expand Down
103 changes: 103 additions & 0 deletions examples/tests/ParameterizedTest.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include "mu/tiny/test.hpp"

// --- 1. Plain data-driven table, the classic pytest.mark.parametrize case ---

namespace {
struct SquareCase
{
int input;
int expected;
};
} // namespace

TEST_GROUP(NumberChecks)
{};

PARAMETERIZED_TEST(NumberChecks, square_matches_expected, SquareCase)
{
CHECK_EQUAL(params.expected, params.input * params.input);
}
PARAMETERIZED_TEST_CASE(
NumberChecks,
square_matches_expected,
two,
(SquareCase{ 2, 4 })
)
PARAMETERIZED_TEST_CASE(
NumberChecks,
square_matches_expected,
three,
(SquareCase{ 3, 9 })
)
PARAMETERIZED_TEST_CASE(
NumberChecks,
square_matches_expected,
negative_four,
(SquareCase{ -4, 16 })
)

namespace {

class FooInterface
{
public:
virtual const char* class_name() const = 0;
virtual int foo(int value) const = 0;
virtual ~FooInterface() = default;
};

class ImplA : public FooInterface
{
public:
const char* class_name() const override { return "ImplA"; }
int foo(int value) const override { return value + 1; }
};

class ImplB : public FooInterface
{
public:
const char* class_name() const override { return "ImplB"; }
int foo(int value) const override { return value + 10; }
};

struct FooParams
{
FooInterface* sut;
const char* expected_name;
};

// One persistent instance per implementation, handed out by address. A
// fresh-per-test SUT is also possible - construct it directly in the case
// expression instead of taking the address of a static.
FooParams impl_a_params()
{
static ImplA impl;
return { &impl, "ImplA" };
}

FooParams impl_b_params()
{
static ImplB impl;
return { &impl, "ImplB" };
}

} // namespace

#define FOO_CASES(testName) \
PARAMETERIZED_TEST_CASE(FooContract, testName, ImplA, (impl_a_params())) \
PARAMETERIZED_TEST_CASE(FooContract, testName, ImplB, (impl_b_params()))

TEST_GROUP(FooContract)
{};

PARAMETERIZED_TEST(FooContract, class_name_matches, FooParams)
{
STRCMP_EQUAL(params.expected_name, params.sut->class_name());
}
FOO_CASES(class_name_matches)

PARAMETERIZED_TEST(FooContract, foo_returns_greater_value, FooParams)
{
CHECK(params.sut->foo(1) > 1);
}
FOO_CASES(foo_returns_greater_value)
18 changes: 18 additions & 0 deletions include/mu/tiny/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,24 @@ extern "C"
#define XFAIL_TEST(group_name, test_name) \
extern void expect_fail_##group_name##_##test_name##_wrapper_c(void); \
void expect_fail_##group_name##_##test_name##_wrapper_c(void)

/**
* @brief Define the C body of a parameterized test.
*
* The function stub is called once per case by the corresponding
* @c PARAMETERIZED_TEST_CASE_C_WRAPPER macro (mu/tiny/test.hpp) in the C++
* bridge file. @p ParamsType must be a type visible to both this file and
* the bridge file - typically a plain struct declared in a shared header.
*
* @param group_name Test group.
* @param test_name Base test name, shared by every case.
* @param ParamsType Type received by value as @c params.
*/
#define PARAMETERIZED_TEST(group_name, test_name, ParamsType) \
extern void parameterized_##group_name##_##test_name##_wrapper_c( \
ParamsType \
); \
void parameterized_##group_name##_##test_name##_wrapper_c(ParamsType params)
#endif

/**
Expand Down
73 changes: 73 additions & 0 deletions include/mu/tiny/test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,56 @@
} /* namespace */ \
void EXPECT_FAIL##testGroup##_##testName##_Test::test_body()

/**
* @brief Define the shared body of a parameterized test.
*
* Write the body once, then list each case with @ref PARAMETERIZED_TEST_CASE.
* Every case expands to an ordinary @ref TEST() named "testName_id" -
* independently registered, filterable by name, and reported on failure like
* any other test. A case failing does not affect its siblings.
*
* @param testGroup An already-declared @ref TEST_GROUP.
* @param testName Base name; each case run as "testName_id".
* @param ParamsType Type passed by value to the body as @c params.
*
* @code{.cpp}
* struct SquareCase { int input; int expected; };
*
* PARAMETERIZED_TEST(NumberChecks, square_matches_expected, SquareCase)
* {
* CHECK_EQUAL(params.expected, params.input * params.input);
* }
* PARAMETERIZED_TEST_CASE(NumberChecks, square_matches_expected, two,
* (SquareCase{2, 4}))
* PARAMETERIZED_TEST_CASE(NumberChecks, square_matches_expected, three,
* (SquareCase{3, 9}))
* @endcode
*
* @see PARAMETERIZED_TEST_CASE
*/
#define PARAMETERIZED_TEST(testGroup, testName, ParamsType) \
void testGroup##_##testName##_body(ParamsType params)

/**
* @brief Register one case of a parameterized test.
*
* Must follow the matching @ref PARAMETERIZED_TEST definition in the same
* translation unit.
*
* @param testGroup Same group passed to the matching @ref PARAMETERIZED_TEST.
* @param testName Same name passed to the matching @ref PARAMETERIZED_TEST.
* @param id Short, unique-within-testName identifier for this case;
* becomes the "_id" suffix of the generated test name.
* @param expr Expression producing a ParamsType value for this case.
*
* @see PARAMETERIZED_TEST
*/
#define PARAMETERIZED_TEST_CASE(testGroup, testName, id, expr) \
TEST(testGroup, testName##_##id) \
{ \
testGroup##_##testName##_body(expr); \
}

/**
* @brief Attach a key/value property to the currently running test.
*
Expand Down Expand Up @@ -281,4 +331,27 @@
expect_fail_##group_name##_##test_name##_wrapper_c(); \
}

/**
* @brief Bridge a C-defined parameterized test body into one case.
*
* @p ParamsType must match the type used in the corresponding C-side @ref
* PARAMETERIZED_TEST definition (mu/tiny/test.h).
*
* @param testGroup Test group.
* @param testName Base name, matching the C-side @ref PARAMETERIZED_TEST.
* @param id Short, unique-within-testName identifier for this case.
* @param ParamsType Type passed by value to the C body.
* @param expr Expression producing a ParamsType value for this case.
*/
#define PARAMETERIZED_TEST_CASE_C_WRAPPER( \
testGroup, testName, id, ParamsType, expr \
) \
extern "C" void parameterized_##testGroup##_##testName##_wrapper_c( \
ParamsType \
); \
TEST(testGroup, testName##_##id) \
{ \
parameterized_##testGroup##_##testName##_wrapper_c(expr); \
}

#endif
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ add_executable(mutiny_test
src/OrderedTestC.test.c
src/Ordered.test.cpp
src/OrderedTestC.test.cpp
src/ParameterizedTest.test.cpp
src/ParameterizedTestC.test.c
src/ParameterizedTestC.test.cpp
src/Preprocessor.test.cpp
src/SetPointerPlugin.test.cpp
src/String.test.cpp
Expand Down
19 changes: 19 additions & 0 deletions tests/src/ParameterizedTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef INCLUDED_PARAMETERIZEDTEST_H
#define INCLUDED_PARAMETERIZEDTEST_H

#ifdef __cplusplus
extern "C"
{
#endif

typedef struct
{
int input;
int expected;
} SquareCase;

#ifdef __cplusplus
}
#endif

#endif /* INCLUDED_PARAMETERIZEDTEST_H */
Loading