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

Re: unit_test_teardown


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), \
     unit_test(test), \
     _unit_test_teardown(test, teardown)


I've added a dummy function that doesn't do anything but it keeps the
increments and decrements of the number_of_test_states in check.

Regards,

Jan







> On Friday 25 October 2013 21:08:02 Jan Paesmans wrote:
>> Hi,
> Hey,
>
>> I'm trying to create a unit test with no setup and a teardown function.
>> Like in the code below:
>>
>> static void teardown_testing(void **state)
>> {
>>         free(*state);
>> }
>>
>> static void testing(void **state)
>> {
>>         *state = malloc(1);
>>         assert_non_null(*state);
>> }
>>
>> int main(int argc, char *argv[])
>> {
>>         (void)argc;
>>         (void)argv;
>>
>>         const UnitTest tests[] = {
>>                 unit_test_teardown(testing, teardown_testing),
>>         };
>>
>>         return run_tests(tests);
>> }
>>
>> This code fails to compile, however this is the way I exepected cmocka
>> to work. Please correct me if I'm wrong.
>> However I think the problem is with the definition of the
>> unit_test_teardown macro.
>> Examining the macro shows that unit_test_teardown(test, teardown) is
>> expanded to:
>> _unit_test_setup(test, setup), \
>> unit_test(test), \
>> _unit_test_teardown(test, teardown)
>>
>> However, there is no setup in the unit_test_teardown macro arguments,
>> causing gcc to give an error about missing setup.
> this is probably a copy and paste error :) I've fixed it. It will be in the 
> next bugfix release.
>
>
> Thanks for reporting!
>
>
> 	-- andreas
>
>
>



Follow-Ups:
Re: unit_test_teardownAndreas Schneider <asn@xxxxxxxxxxxxxx>
References:
unit_test_teardownJan Paesmans <jan.paesmans@xxxxxxxxx>
Re: unit_test_teardownAndreas Schneider <asn@xxxxxxxxxxxxxx>