简体   繁体   English

如何使用 GoogleTest 测试仅在 CPP 文件中定义的函数

[英]How to test a function only defined in CPP file with GoogleTest

I'm new with GoogleTest framework and I have an issue related to testing utility methods that are only defined in the CPP file.我是 GoogleTest 框架的新手,我遇到了与仅在 CPP 文件中定义的测试实用程序方法相关的问题。 In the following example I would like to test the function baz without exposing it through the .hpp file.在下面的示例中,我想测试函数baz而不通过.hpp文件公开它。 What would be the best approach?最好的方法是什么?

// .hpp
namespace ns {
  void foo();
  void bar();
}


// .cpp
#include ".hpp"

void baz() {
}

namespace ns {
  void foo() {
    baz();
  }

  void bar() {
    baz();
  }
}

You don't need to expose baz through the .hpp file.您不需要通过 .hpp 文件公开baz You can declare void baz();您可以声明void baz(); in any file before its usage.在使用之前的任何文件中。 Even inside a function, like即使在函数内部,例如

void foobar() {
  void baz();
  baz();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GoogleTest:如何跳过测试? - GoogleTest: How to skip a test? 如何使用googletest通过引用参数传递来测试void函数? - How to test void function with pass by reference parameter using googletest? 在命名空间中定义已在 cpp 文件中定义的 function - Define in a namespace a function already defined in a cpp file 如何在.cpp 文件中 function 的返回类型(签名)中使用在 header 文件中定义的别名? - How can I use an alias thats defined in a header file, in the return type (signature) of a function in the .cpp file? 我可以将参数传递给 googletest 测试函数吗 - Can I pass parameters to googletest test function 使用 GoogleTest 测试从文件中读取 - Test reading from a file using GoogleTest C++ gmock - 我们如何在单元测试 cpp 文件中读取/获取 cpp 文件的函数的参数值 - C++ gmock - How we can read/get the parameter value of a function of cpp file in unit test cpp file 从.cpp文件中调用的函数,该文件在.c文件中定义 - function called from .cpp file which is defined in in .c file 如何在标头和 .cpp 文件中正确声明返回自己定义类型的函数? - How do I properly declare a function returning own-defined type, both in the header and the .cpp file? GoogleTest仅在.h文件中定义时才起作用 - GoogleTest only work when definition is in .h file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM