Testing C API using gtest

5,940 views
Skip to first unread message

Rohit Sharma

unread,
Jan 26, 2011, 4:02:49 AM1/26/11
to Google C++ Testing Framework
Hello,
I should write an automation framework for a C API of a
proprietory product
Would it be possible for me to test a C based API using Cpp based
testing framework.
I am new to this. So any suggestions from people who cracked similar
problems are highly appreciated.

Regards
Rohit

Josh Kelley

unread,
Jan 26, 2011, 1:02:56 PM1/26/11
to Rohit Sharma, Google C++ Testing Framework
On Wed, Jan 26, 2011 at 4:02 AM, Rohit Sharma <rohit.a...@gmail.com> wrote:
>
> Would it be possible for me to test a C based API using Cpp based
> testing framework.

Yes. Since C++ code can call C code, you can use Google Test (or
another C++ testing framework) to test C code.

One potential problem with testing C code is that unit testing
(testing each component in isolation) can be harder, since C doesn't
directly support polymorphism. (In an OO language, you can use
polymorphism to replace components that aren't being tested in a
particular test with fake or mock objects.) There are a few
techniques to work around this in C, but if the API calls you're
testing are relatively small and relatively isolated from each other,
it may not even be an issue.

--
Josh Kelley

Keith Ray

unread,
Jan 26, 2011, 1:05:25 PM1/26/11
to Rohit Sharma, Google C++ Testing Framework
You test code will live in a C++ source file ( .cc or .cpp, whatever
your convention) but there isn't any problem with testing sources.

You probably want to make sure your C header files say something like
the following, in order to link properly.

#if __cplusplus
extern "C" {
#endif

your header file contents

#if __cplusplus
}
#endif

--
--
C. Keith Ray
 Web: http://industriallogic.com
 Twitter: @CKeithRay, @IndustrialLogic

Amplify Your Agility
Coaching | Training | Assessment | eLearning

Mike Long

unread,
Jan 26, 2011, 1:59:56 PM1/26/11
to Rohit Sharma, Google C++ Testing Framework
Hello,

I have written a blog post with some tips you might find useful:
http://meekrosoft.wordpress.com/2009/11/09/unit-testing-c-code-with-the-googletest-framework/

I have also written a fake function framework that can help when mocking out dependencies for C programs:
https://github.com/meekrosoft/fff

Look in the examples directory for some full examples of how to make it work.

Good luck!

/Mike

James Grenning

unread,
Feb 3, 2011, 8:16:46 AM2/3/11
to Google C++ Testing Framework
Hi

You can test C from a C++ framework.

I use CppUTest for testing C. It hides the C++ syntax behind macros
so that it is easier to use for non-C++ programmers. I feature its
use in my Book Test-Driven Development for Embedded C (http://
pragprog.com/titles/jgade/)


James

----------------------------------------------------------------------------
James Grenning TDD for Embedded C is in Beta
ja...@renaissancesoftware.net http://pragprog.com/titles/jgade/
www.renaissancesoftware.net
www.renaissancesoftware.net/blog
www.twitter.com/jwgrenning

James Grenning

unread,
Feb 3, 2011, 8:25:24 AM2/3/11
to Google C++ Testing Framework
If you don't want to or cannot modify the C header files, you can
limit the C++ dependency in your test files like this:

------ xxxTest.cpp ------
....

extern "C"
{
//Your header files for code with C-linkage go here.
}


.....
----- end xxxTest.cpp -----


I think Keith's advice on header files is appropriate when the
production code has both C/C++.

James

-----------------------------------------------------------------------------
On Jan 26, 12:05 pm, Keith Ray <keith....@gmail.com> wrote:
> You test code will live in a C++ source file ( .cc or .cpp, whatever
> your convention) but there isn't any problem with testing sources.
>
> You probably want to make sure your C header files say something like
> the following, in order to link properly.
>
> #if __cplusplus
> extern "C" {
> #endif
>
> your header file contents
>
> #if __cplusplus}
>
> #endif
>

SKiran

unread,
Dec 27, 2013, 4:00:19 AM12/27/13
to googletes...@googlegroups.com
Hi All,

  I'm newbee to GTest. Is there any links or books available to know more about GTest and try few example.
eagerly waiting for the response.

Thanks,
SKiran

Zhanyong Wan (λx.x x)

unread,
Dec 27, 2013, 11:48:06 AM12/27/13
to SKiran, Google C++ Testing Framework
Jeff Langr just wrote a book about TDD in C++:


It's not about gtest per se, but the examples are using gtest.

I read the book and liked it.  He explains things very well and offers practical advice.


--
 
---
You received this message because you are subscribed to the Google Groups "Google C++ Testing Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googletestframe...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Zhanyong
Reply all
Reply to author
Forward
0 new messages