[英]How to do Google test for function which are not a part of class?
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.
我可以通过创建类的 obj 来访问头文件中的函数,但无法访问仅在源文件中的函数。
Please guide me how to access these function.请指导我如何访问这些功能。
Thanks in advance!.提前致谢!。 Kiran JP
基兰JP
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.
通过将其声明为静态或通过在 C++11 或更高版本的匿名命名空间中声明/定义它。
You will need to add the declaration of those functions to a header file, either to your existing header files, or to a new header file.您需要将这些函数的声明添加到头文件中,或者添加到现有的头文件中,或者添加到新的头文件中。
For example, say you have this function in your class.cc
file without any declaration in class.h
file:例如,假设您在
class.h
文件中有此函数,而在class.cc
文件中没有任何声明:
int SomeFunction(int a, int b){
//...
}
You should add the following line to a header file.您应该将以下行添加到头文件中。 Let's call it
header.h
:我们称之为
header.h
:
// header.h
int SomeFunction(int a, int b);
Then modify your test file to include header.h
:然后修改您的测试文件以包含
header.h
:
#include "header.h"
TEST(SomeFunction, Test1) {
int c = SomeFunction(1,2);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.