GithubHelp home page GithubHelp logo

a question for help about cmockery2 HOT 5 CLOSED

lpabon avatar lpabon commented on August 25, 2024
a question for help

from cmockery2.

Comments (5)

lpabon avatar lpabon commented on August 25, 2024

This is the right place for a question. I'm checking your code now...

from cmockery2.

lpabon avatar lpabon commented on August 25, 2024

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.

jinwuxia avatar jinwuxia commented on August 25, 2024

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.

lpabon avatar lpabon commented on August 25, 2024

@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.

jinwuxia avatar jinwuxia commented on August 25, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.