First time trying to use cpputest - struggling with basics

51 views
Skip to first unread message

ST

unread,
Oct 24, 2022, 6:30:43 AM10/24/22
to cpputest
Hello everyone,

I've been studying cpputest for the better part of a week now, but am still having difficulties. After giving up on spending time on various examples and starter projects that failed, I figured I'll try to create a simple example myself.

The basics:
* I'm working inside a docker container with a centos 7 variant.
* I've successfully compiled and run make install on cpputest 3.8

At this moment I've tried to create a simple project with 2 files:
  main.cpp
  module.cpp
inside module.cpp I have 2 functions:

  int iNeedTesting(int arg)
  {
    return privateFunc(arg) / 2
  }

  int privateFunc(int arg)
  {
    return arg;
  }

Now I am trying to create a test folder with main.cpp following these instructions, but when writing the testing code, I'm not sure how to have cpputest recognize my module.
Do I need to link it with the precompiled O file, meaning testing need to depend on full building of the project (Unfortunately that would have to be the case in the bigger project I'm working on)?

Also, if I'm already posting, might I inquire if it is possible to mock privateFunc so it can return various outputs to test iNeedTesting?

Any help would be greatly appreciated to get me going underway.

Thanks
ST

Bas Vodde

unread,
Oct 27, 2022, 9:05:14 AM10/27/22
to cppu...@googlegroups.com

Hi ST,

You’ll need to link your tests, your application code, and the library (.a) file from CppUTest. Then that ought to produce an executable file.

Related to privateFunc. If it is just a C function, then usually we recommend to use function pointer stubbing. This means you need to change
your production code to use a function point. Something like:

int (*privateFunc)(int) = privateFuncImp

Then you can replace it with UT_PTRSET in the test.

I think there are examples of this online also.

Thanks,

Bas


--
You received this message because you are subscribed to the Google Groups "cpputest" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cpputest+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cpputest/fc850a44-4763-4ccc-8e76-475d0d9a8199n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages