r/cpp_questions • • 6d ago

SOLVED 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 !

4 Upvotes

12 comments sorted by

5

u/the_poope 6d 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.

2

u/Titourterelle 6d ago

I use a similar logic and it seems to work. I just separated src files between exec and lib because my lib doesn't need exec files but my test exec have all of them. Thanks

2

u/bert8128 6d 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.

3

u/Titourterelle 6d ago

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

Thanks for the tips

1

u/jumurtka 4d ago

TIL. Thanks.

1

u/conundorum 18h ago edited 18h ago

For the record, "write failing tests first" is from test-driven development specifically, where you write the test before the code, and then write the code so that the test will pass. It doesn't just mean that all tests should fail unless the code is rewritten; if a function already functions properly, then any test which fails that function is a bad test.

template<typename T>
T add(T a, T b) { return a + b; }

// Test designed to fail.
void Test::add() {
    int a = 5, b = 10;

    TestAssert(add(a, b) == a + (2 * b));
}

// When the test fails, the issue here will clearly be the test, not the code.
// If the code is "fixed" so that the test passes, then `add()` will no longer perform proper math.

Ultimately, the goal is that the test verifies that the function produces the intended result. TDD determines the test case and its desired result before the code is capable of producing the result, and thus writes a test guaranteed to fail. The code is then fixed to produce the desired result, thus passing the test. It's less about making the test pass, and more about using the test to mandate the proper result. Thus, if the code already exists before the test is written, it's okay for the test to pass right off the bat; that just means that the code is correct.

template<typename T>
T add(T a, T b) { return a + b; }

// A better test.
void Test::add() {
    int a = 5, b = 10;

    TestAssert(add(a, b) == a + b);
}

(For comparison, in TDD, you would make add() as a codeless dummy first (e.g., return 0; instead of an actual body), and then write Test::add(). (Because Test::add() can't be compiled if add() itself doesn't exist, so we need a compilation dummy.) We thus have a failing test, where the test compares the function result to a result that's known to be correct. You then implement add(), compile it to check for errors, and run the test to check for valid results. Failing tests are never a goal, they're just the result of making the test first and the code second.)

2

u/Discordbothost 6d ago

for private methods, start by exercising them through the public operation and checking the result. then you can change the internal implementation without rewriting every test. if a helper is substantial enough to need its own tests, extract it into a small internal component.

1

u/Titourterelle 6d ago

Ok thanks, I succeed to configure gtest with CMake in a proper way (I think) so now I need to write my first tests.

1

u/Discordbothost 6d ago

start with functions whose expected output you can write by hand, plus any bug you fixed once. also EXPECT_EQ keeps going after a failure, ASSERT_EQ returns.

1

u/Titourterelle 6d ago

I think I understood but maybe I didn't clearly get what you mean by "you can write by hand"? And I already read the beginning of the Google test documentation but thanks anyway

1

u/Discordbothost 6d ago

i mean pick cases where you already know the answer without running the code, like parse("2+3") is 5. then the test checks behaviour, not whatever the code does today.

1

u/Titourterelle 6d ago

Oh I see