Mock automation

61 views
Skip to first unread message

Bob Gong

unread,
Jul 22, 2009, 12:00:38 AM7/22/09
to Cmockery
I am trying to introduce Cmockery into current project (around 2
millions of legacy code, mixed with C and C++ code), definitely it is
impossible to wrote mock code manually for one thousand of exist
header files. So I am trying to simplify the rule to mock and develop
a script to generate mock code base on header files. Now, the script
works for most of code, but got compilation errors like "can not
convert void* to enum" in case of return type is enum, and "can not
convert float to void*" in case of argument type is float and etc...

Is anyone interested in this? Probably, I can summary what I did and
post the code later this week.

Regards,
-Bob

Alexander Demin

unread,
Jul 22, 2009, 4:17:29 AM7/22/09
to cmoc...@googlegroups.com
Bob,

I also work with lots of legacy code on C и C++ and try to inject Google Test and Cmockery tools into that code.

Could you please give an example how do you generate mocks from headers?

Alexander

Bob Gong

unread,
Jul 22, 2009, 4:52:13 AM7/22/09
to Cmockery
Hi Alexander,

Firstly, I extend Cmockery to support bunch of macros:

Use check_expected_and_optional_assig(parameter) in mock code.

Use following macros in test code.
will_assign_string(function, parameter, string)
will_assign_string_count(function, parameter, string, count)
will_assign_memory(function, parameter, memory,size)
will_assign_memory_count(function, parameter, memory, size, count)
will_assign_struct(function, parameter, assign_function,value)
will_assign_struct_count(function, parameter, assign_function, value,
count)

Then a simple rule to generate mocks from headers is:
{return_type} function_name({arg1_type} arg1_anme,{arg2_type}
arg2_name);
=>
{return_type} function_name({arg1_type} arg1_anme,{arg2_type}
arg2_name)
{
check_expected_and_optional_assign(arg1_name);
check_expected_and_optional_assign(arg2_name);
return ({return_type})mock();
}

In test code, it is forced to expect all arguments, and modify
arguments optionally.

Regards,
-Bob

On Jul 22, 4:17 pm, Alexander Demin <ade...@gmail.com> wrote:
> Bob,
> I also work with lots of legacy code on C и C++ and try to inject Google
> Test and Cmockery tools into that code.
>
> Could you please give an example how do you generate mocks from headers?
>
> Alexander
>

Alexander Demin

unread,
Jul 22, 2009, 5:30:36 AM7/22/09
to cmoc...@googlegroups.com
Bob,

Please correct if I am wrong. As I understand the idea of your extension is to add the mock mechanism not only for the function return value but also for its parameters which could carry out information from a function, isn't?

So by default "check_expected_and_*()" macro works similar to the regular "check_expected()" and just check the value of a parameter. But if a test has "will_assign_*()" macro for the parameter the mock function will additionally modify that parameter in the "check_expected_and_*()" macro accordingly to the "will_assign_*()" macro used. 

So if a function parameter is a pointer we can automatically mock the value of the parameter.

And also you have a script generating mock function templates.

Alexander

Bob Gong

unread,
Jul 22, 2009, 6:13:58 AM7/22/09
to Cmockery
Alexander,

Yes. You are exactly right!

Regards,
-Bob

On 7月22日, 下午5时30分, Alexander Demin <ade...@gmail.com> wrote:
> Bob,
> Please correct if I am wrong. As I understand the idea of your extension is
> to add the mock mechanism not only for the function return value but also
> for its parameters which could carry out information from a function, isn't?
>
> So by default "check_expected_and_*()" macro works similar to the regular
> "check_expected()" and just check the value of a parameter. But if a test
> has "will_assign_*()" macro for the parameter the mock function will
> additionally modify that parameter in the "check_expected_and_*()" macro
> accordingly to the "will_assign_*()" macro used.
>
> So if a function parameter is a pointer we can automatically mock the value
> of the parameter.
>
> And also you have a script generating mock function templates.
>
> Alexander
>

Alexander Demin

unread,
Jul 22, 2009, 6:27:02 AM7/22/09
to cmoc...@googlegroups.com
Bob,

From my point of view this functionality organically fits in to the cmockery API and extends it nicely. I'd like personally to play with your patch. If it's big to be posted here you could stick it at the files section of the discussion group page.

Alexander

Bob Gong

unread,
Jul 22, 2009, 9:06:10 PM7/22/09
to Cmockery
Alexander,

I will merge latest code and post my patch later this week.

Could you explain how do you inject both Google Test and Cmockery in
your project?
Are you using them independently? Google Test for C++ code and
Cmockery for C code?

Regards,
-Bob

On Jul 22, 6:27 pm, Alexander Demin <ade...@gmail.com> wrote:
> Bob,
> From my point of view this functionality organically fits in to the cmockery
> API and extends it nicely. I'd like personally to play with your patch. If
> it's big to be posted here you could stick it at the files section of the
> discussion group page.
>
> Alexander
>

Bob Gong

unread,
Jul 23, 2009, 5:16:23 AM7/23/09
to Cmockery
Alexander,

Files are uploaded at the files section.
I should rename the .c file... seems can not rename it after
uploaded...

Regards,
-Bob

Alexander Demin

unread,
Jul 23, 2009, 11:18:39 AM7/23/09
to cmoc...@googlegroups.com, Cmockery
Bob,

Unfortunatelly I do not do any automated test generation stuff as you
do with cmockery. I just write tests for new code and for bug fixes. I
use Google Test/Mock for C++ code and cmockery for C code.

Alexander



23.07.2009, в 2:06, Bob Gong <gongx...@gmail.com> написал(а):

Alexander Demin

unread,
Jul 24, 2009, 5:13:26 AM7/24/09
to Cmockery
Bob,

I will play with your patch. Thanks. It should be useful.

Alexander
Reply all
Reply to author
Forward
0 new messages