How to do Google test for function which are not a part of class?

Issue

Am performing Google test, Here am facing one challenge.

In my project some of the functions are not included in the header file, they are directly included in the source file. I can access the functions which are in header file by creating obj of the class but am not able to access which are only in source file.

Please guide me how to access these function.

Thanks in advance!.
Kiran JP

Solution

Declare them extern in your test code.

Example. Let’s say you have a source file like this:

// Function declared and defined in .cpp file
void myFunction() {
    // implementation
}

Then you could go ahead and do the following in your test code:

extern void myFunction();

TEST(MyTest) {
    myFunction();
}

Unless the function was explicitly declared with internal linkage. Either by declaring it static or by declaring/defining it inside an anonymous namespace in C++11 or above.

Answered By – Tohnmeister

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published