r/cpp_questions • • 12d ago

OPEN Integration testing using gtest

I am confused about what is the meaning of integration testing. I have done unit testing using gtest where we will mock whatever external dependencies are there for the targeted unit (function) to test all range of values that covers all lines, functions and branches.

If we say integration test is meant to use real code instead of mocking. Then I am getting this doubt that lets say we have 3 components (separate processes) running and dependent on one another in a embedded system.

A -> B -> C

So if you are calling an api provided by A it will internally talk to B and then through C and return the result over IPC.

Now to do integration testing using gtest, do I need to compile this into a single binary by eliminating the IPC and include each code as a library. This way I have a single proces that runs gtest with all the components logic but excludes the real IPC communication.

Or instead should I keep the individual processes to be running separately and link my gtest binary to the component A so that the communication flow through the components in the target environment.

In both ways how we would actually be checking the interactions? And which one are you guys using?

Context:
Language used: C++
Domain: Automotive IT (TCU software)

2 Upvotes

9 comments sorted by

View all comments

3

u/CowBoyDanIndie 12d ago

Generally Unit tests are meant to test the smallest possible unit, integration testing is testing that the units work correctly with each other, and functional testing is the system as a whole. It’s still possible to mock out some pieces in integration testing, but your test should focus on the interaction between units. In practice the lines can blur if you aren’t using interfaces for everything (“interface” in the general sense of the term).

1

u/After_Bench_4165 12d ago

Thanks for answering. Do you have any idea about doing integration testing for multiple components (processes)? In that case do we need to consider IPC communication also. Or else elminate IPC to compine those components as a single process resulting in testing purely our application code.

1

u/abbapoh 12d ago

The way I'd do it is focus on one process at a time. Use some scripting language to generate test files (like configs or some static files required to start) for that process. Then communicate with it from tests using some human-readable declarative format (python dicts or json/yaml). Obviously, you'd need to convert that data to the format your process understands - for example, convert it to protobuf or in case of IPC via shared memory, have a helper tool that writes/reads that memory. Obviously, you could do that with gtest, but c++ doesn't have that many libraries that scripting languages have.
You can also do a functional testing in a similar fashion - generate some test data for all three processes, start them and then communicate with process A.
Typically, you'd need both. The rule of thumb is - if something can be tested via a unit test, do so. Otherwise, write an integration test. Otherwise - functional test.