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

Re: unit_test_teardown


Hi,

I've found out why your test works.
If you could just rerun the fixure_test.c with only the teardown test
enabled, then you'll see that it stops with an assert in cmocka.c at
line 1780.
This is due to the fact that for each setup, a counter is incremented,
number_of_tests. And in the teardown, this counter is decremented, after
making sure it is not already zero.
If you return the fixture_test with only the teardown test enabled, then
you'll have the error as described.
In the fixture_test.c file, I've just put the line 32 in comments and
then the test fails.

Regards,

Jan
> On Monday 28 October 2013 22:15:43 Jan Paesmans wrote:
>> Hi,
>>
>> I've updated my git repo and tried running my test.
>> It fails now with the following message:
>>
>> number_of_test_states
>> /home/janp/src/cmocka/src/cmocka.c:1780: error: Failure!
>>
>> I've traced the problem to the variable number_of_test_states. This
>> variable is incremented for each setup for a test and decremented for
>> each teardown of a test.
>> In case of a unit_test_teardown, there is no setup and the assert at the
>> indicated line is triggered.
>> I've tried changed the macro to something line this:
>>
>>  #define unit_test_teardown(test, teardown) \
>> +    _unit_test_setup(test, NULL), \
>>      unit_test(test), \
>>      _unit_test_teardown(test, teardown)
>>
>>
>> However, that resulted in the same problem. There is a check before the
>> switch statement, to continue if the function is NULL.
>> So I've resorted to this dirty hack:
>>
>> diff --git a/include/cmocka.h b/include/cmocka.h
>> index 0df2e90..e9138b8 100644
>> --- a/include/cmocka.h
>> +++ b/include/cmocka.h
>> @@ -1310,8 +1310,11 @@ int run_test(#function);
>>  #define _unit_test_teardown(test, teardown) \
>>      { #test "_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_TEARDOWN }
>>
>> +static void dummy(void **state) { (void)state; }
>> +
>>  /** Initializes a UnitTest structure with a teardown function. */
>>  #define unit_test_teardown(test, teardown) \
>> +    _unit_test_setup(test, dummy), \
> This line isn't needed. I've added tests for these functions now.
>
>
> See 
> http://git.cryptomilk.org/projects/cmocka.git/commit/?id=5c8a0fb3a07912aad0121b948092cb108a958ec9
>
>>      unit_test(test), \
>>      _unit_test_teardown(test, teardown)
>>
>
> 	-- andreas
>