A-mit
A-mit

Reputation: 31

c cmocka running the same test function with different parameters

I'd like to write a test in cmocka and run it few times with different parameters each time (so I could test different input cases). Something like Python's decorators @parameterized.parameters. There are ways I can think of, like:

  1. I can always use a common aux function and extract to it the main logic of the test. Then call this aux function from different tests.
  2. I can declare an array of input parameters at the top of the test, then run the test logic in a loop and iterate on the array.
  3. Do the same thing only by declaring the input parameters in the setup function.
  4. Write the test in a generic way, write setup functions for each parameters set, then execute the same test function while each time suppling a different setup function. That way the test will receive the parameters values from the setup. I am not sure this is a good methodology though

The goal is better maintainability and scalability.

Simple example to a test to refactor: Let's say I have a function bool is_even(int a) and I want to test it. In the tests file:

static void test_returned_val_is_even(void **state) {
   bool rc = is_even(2);
   assert_true(rc);
}

p.s. Let's pretend the test does much more than just calling is_even :)

I was wandering what are other ways are out there and if there a methodology better than another and why :) Thanks in advance.

Upvotes: 3

Views: 597

Answers (0)

Related Questions