ThrowTheSwitch / CMock

CMock - Mock/stub generator for C
http://throwtheswitch.org
MIT License
666 stars 272 forks source link

CMock not able to mock stdio.h #218

Open gpenghe opened 5 years ago

gpenghe commented 5 years ago

Hi, I am trying to mock file operation functions like fopen etc. So I added the following line to my test file:

#include "mock_stdio.h"

However, calling fopen_IgnoreAndReturn failed as ceedling complains it is not defined.

So does CMock support mocking C library functions? If so how? Thanks.

Xenoamor commented 5 years ago

What does the contents of your mock_stdio.h file look like? Did CMock throw any errors or warnings when you ran it against stdio.h?

ajones-pillar commented 5 years ago

I'm trying to do the same thing. If I #include "mock_stdio.h", I receive a message "ERROR: Found no file 'stdio.h' in search paths". I've tried a number of configurations (including adding my target system's .h files to the source path, which avoided the error above, but had its own issues), but am thinking this might not be possible.

If this isn't possible, can we have that clarified and perhaps added to the documentation?

gpenghe commented 5 years ago

@Xenoamor Here is the content of mock_stdio.h:

/* AUTOGENERATED FILE. DO NOT EDIT. */
#ifndef _MOCK_STDIO_H
#define _MOCK_STDIO_H

#include "stdio.h"

/* Ignore the following warnings, since we are copying code */
#if defined(__GNUC__) && !defined(__ICC) && !defined(__TMS470__)
#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 0)))
#pragma GCC diagnostic push
#endif
#if !defined(__clang__)
#pragma GCC diagnostic ignored "-Wpragmas"
#endif
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC diagnostic ignored "-Wduplicate-decl-specifier"
#endif

void mock_stdio_Init(void);
void mock_stdio_Destroy(void);
void mock_stdio_Verify(void);

#if defined(__GNUC__) && !defined(__ICC) && !defined(__TMS470__)
#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 0)))
#pragma GCC diagnostic pop
#endif
#endif

#endif
gpenghe commented 5 years ago

@ajones-pillar I checked my project.yml. Under ":paths:->:include:", there is a line of - /usr/include. Hope it helps.

Xenoamor commented 5 years ago

Make your own stdio.h that has the function declarations that you are interested in Then run CMock against that

tmilburn commented 4 years ago

I'm also having issues mocking fopen.

When I create my own stdio.h with fopen in it I get the following error

ERROR: Test executable "test_paxlockDevices.out" failed. Produced no output to $stdout. And exited with status: [0] (count of failed tests). This is often a symptom of a bad memory access in source or test code.

I guess unity / cmock is using fopen internally.

kristyan-osborne-paxton commented 1 year ago

In case anyone is looking for an answer to this, what's been mentioned above is the correct way to do it.

Create your own header file with the definition for the library function. The thing that is probably catching a lot of people out is it's dependant on the c library you are using. If you are using GCC most function definitions will include one of the "THROW" macros. These are used for compatibility with c++. cmock is unable to create mocks if one of the throw macros is used. To solve this just redefine the one being used on your function.

#undef __THROW
#define __THROW

int clock_gettime (clockid_t __clock_id, struct timespec *__tp) __THROW;