简体   繁体   English

如何对不属于课程的功能进行谷歌测试?

[英]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.

相关问题 你如何模拟作为测试 gmock 的那个类的一部分的函数? - How do you mock a function that is part of that class under test gmock? 如何使用谷歌测试测试 public void function 调用相同 class 的 void private function - how to test public void function which calls void private function of the same class using google test 如何使用接口指针调用方法,接口指针是其具体类的一部分而不是接口的一部分 - How do I call method using interface pointer which is part of its concrete class and not part of interface 如何模拟将对象引用作为参数的函数 - 谷歌测试 - How to mock a function which takes an object reference as parameter - google test 如何使用谷歌测试测试 class? - How to test a class with google test? Google测试:使用现有测试夹具类的参数化测试? - Google Test: Parameterized tests which use an existing test fixture class? 如何找出成员函数所属的类 - How do I find out which class a member function belongs to 如何在模板化的类中模拟函数? - How do I mock a function in a class which is templated? 带有属于类的向量的函数不起作用,并且没有编译器错误 - function with vectors which is part of a class is not working and no compiler errors 如何模拟Google Test中公共方法正在使用的类中的私有方法? - How to mock private methods in a class which is being used by public methods in Google Test?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM