[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Supplying test data


Hi,

I'm currently wondering how to supply different test data to the same test function.

To elaborate, suppose I have the standard arrangement of a test for a function with an input, say "is_prime(int)".

   void test_is_prime(void** state) {
        int number = (int)*state;
        assert_true(is_prime(number));
   }

   int main(void) {
        const UnitTest tests[] = {
            unit_test(test_is_prime /* I want to pass 1 */),
            unit_test(test_is_prime /* I want to pass 2 */),
            unit_test(test_is_prime /* I want to pass 17 */),
            // etc.
        };
        return run_tests(tests);
   }

Right now I can't do this using parameters in the test list, as although I know I can pass setup and teardown functions, I cannot define the input to these functions on a case by case basis. Therefore the setup functions cannot tell what context within the text list they are run in and pass the appropriate value. They are single-use only.

Instead, in this case I would create an array of parameters and loop over them in the test. But in a less trivial case with some real setup and teardown to be done, I would have to call the setup and teardown functions myself within the loop. It also means the test result of N tests are flattened down into one. This solution seems to just sidestep the problem by not using the setup and teardown feature.

Rather, I would like to be able to provide data in the test case array like this:

   void setup(void** state, void* context) {
        *state = context; // for example
        // possibly other stuff here
   }

   void teardown(void** state, void* context) {
        // stuff using context here
   }

   int main(void) {
        const UnitTest tests[] = {
            unit_test(test_is_prime,1),
            unit_test(test_is_prime, 2),
            unit_test(test_is_prime, 17),
            // or
            unit_test_setup_teardown(test_something, &caseX, setup,
   teardown),
            unit_test_setup_teardown(test_something, &caseY, setup,
   teardown),
            // etc.
        };
        return run_tests(tests);
   }

That seems like an obvious thing to want to do, so have I missed a way to do this?

Cheers,

Nick