PlatformIO Community

Parametrized tests

Hello, I created unit tests to test my code. Unfortunately I have a lot of duplicated tests, so I would like to use parametrized tests. Is there any way to use their in

I’m not entirely sure if this is what you’re asking, but you can refactor your C/C++ code to replace constants with a function parameter, and then run the new test function with abitrary new parameters in a loop.

Say the original function is

That can be rewritten and expanded to

void generic_addition_test(int expected, int a, int b) {
   TEST_ASSERT_EQUAL(expected, calc.add(a, b));

void test_function_calculator_addition(void) {
   generic_addition_test(32, 25, 7);
   //etc. etc. 
   int testcases[2][3] = {
       { 32, 25, 7},
       { 3, 2, 1}
   for(int i=0; i < 2; i++) {
      generic_addition_test(testcases[i][0], testcases[i][1], testcases[i][2]);

Yes, that is what I mean. Thanks. I have one more question what means void in argument of the test_function_calculator_addition?

void indicates that the function does not take any parameters.

In C, it’s mandatory to use void. In C++, it’s optional. You can omit it and just write:

void test_function_calculator_addition() {

Thanks for any reply. I know everything I wanted.