Comments (5)
This is the right place for a question. I'm checking your code now...
from cmockery2.
Mocking is done by replacing during linking the actual function with your own. So you need to move funcb in func.c to another file, like funcb.c. Then build your unit test without including funcb.c, that way the linker will pick funcb() from test.c.
Here is an example:
$ ls
func.c funcb.c test.c
$ gcc -o func_unittest func.c test.c -lcmockery
$ ./func_unittest
Let me know if that helps
from cmockery2.
I have tried this way as you said. It does work well. Thank you.
I also sovle this problem by adding macro to real funcb. Let the complier does not compile the real funcb but the mocked funcb.
//func.c
# ifndef UNIT_TESTING
void funcb(int para)
{
para = para+10;
printf("real funcb\n");
}
#endif
void funca()
{
int msg = 10;
funcb(msg);
}
in test.c, #define UNIT_TESTING 1
But , from our users's view, the method you said or my macro solution above, has slightly changged the structure of the tested program files.
I think that whether cmockery can provide a internal mechanism to avoid modiying the structure of the tested program files, neither needing splitting the function nor adding macro explicitly.
I don't know wheter my thought is reasonable or not. Just only express my idea without other purpose.
Thank you again for your reply.
from cmockery2.
@j123kaishichufa No problem at all. Your request is a good one, but C language make it really difficult to do this.
Instead what I have done in my projects is only mock external libraries. If I have a func.c
and in that file I have funca()
and funcb()
, I would not mock them. Instead, if funca()
calls funcx()
which is in another directory or library, then I would create a mock_funcx()
.
Again, the goal is to make it simple and able to be maintained by others. Let me know if you need any other help.
from cmockery2.
@lpabon
I think i have gotten your idea. Cmockery2 is really an lightweight test framework for unit test. It easy to use.
Thank you.
from cmockery2.
Related Issues (13)
- make install error with master HOT 4
- Document a simple test
- Debian Package HOT 1
- Add coveralls.io support HOT 1
- Adding full support for SonarQube HOT 8
- Update FreeBSD distinfo to 1.3.9
- [CFakeLib] Would you like to add the library or link it? HOT 2
- expect_check() and expect_in_set() are broken HOT 1
- cast_to_largest_integral_type() strips 64-bits values down to 32-bits
- Google has revived it??
- Profiling output issue in Fedora rpms HOT 2
- Update installation documentation HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cmockery2.