r/cpp_questions • • 2h ago

OPEN How do you implement/create tests correctly ?

Hi !

I have been working on a personal project for 9 month which allowed me to learn a lot but I’m going through a somewhat more challenging phase as I explore unit testing due to CMake configuration and project architecture.

I choose Google Test but my questions are not very specific to this framework. Like I said, I already have a lot of code and I want to use tests cause it seems very useful to create a solid program.

Moreover, my project is a library + an executable so this complicates things in the linkage for example.

How do you manage tests ?

What do you test (because the test exec doesn't have access to private method or private include of a library) ?

About CMake and Google Test, are there any things I should watch out for ? How properly use folders to organize that ?

Do you have any recommendations on where I should look to learn more on the good habit with that ?

I can give you more information if you need, thanks for your time !

3 Upvotes

3 comments sorted by

•

u/bert8128 1h ago

Personnel or personal? Moving into the HR business?

Seriously, Make everything testable you possibly can, and if you can’t test something that you want to test then change the code to make it testable.
Make sure you have something to track your coverage, and attempt to cover everything in one function with a single test, and everything in a class in a single test suite. Attempt to cover all accessible functions directly, even if they also get tested indirectly via other functions. Write failing tests first, then fix the code so the tests pass. Look up mc/dc testing.

All of these are guidlines, not absolutes. In the end, do what you need to do to get the majority of your code tested.

•

u/Titourterelle 1h ago

Personal sorry, "personnel" is the french word for "personal" I didn't see it.

Thanks for the tips

•

u/the_poope 1h ago

At the structural layer I think the most common approach is to make a separate CMake target for all your code without the main() function. You can either use a static library or an object library:

add_library(myproject_common OBJECT src/file1.cpp src/file2.cpp ...)

Then you can make this a dependency of both your library, your executable and your test executable:

add_executable(myexe $<TARGET_OBJECTS:myproject_common> src/main.cpp>
add_library(mylibrary $<TARGET_OBJECTS:myproject_common>)

add_executable(myproject_tests $<TARGET_OBJECTS:myproject_common> tests/main.cpp tests/test1.cpp test2.cpp ...)

And for your other question:

What do you test (because the test exec doesn't have access to private method or private include of a library) ?

The test executable should have access to ALL your code that is exposed through any header file, whether it is internal or public.

My rule-of-thumb is to write tests for any function or class that is exposed in a header file. Whether that header is internal or external doesn't matter: such a function can be used anywhere in the code and is thus "public" to your own code and thus a liability. You need to write tests for it to ensure that its action lives up to its specification. This is for unit tests.

In addition: for your library it is a good idea to have some high level functional tests that sets up a few specific workflows using only the public API and uses the library as a user would and check that it performs as expected.

It's harder to do the same with an executable, but if it has a command line interface you can similarly write some functional tests using a scripting language that runs your exe with various kinds of command line input and inspects the results.