简体   繁体   English

对共享库的私有函数进行单元测试

[英]Unit testing for private functions of a shared library

I was thinking of if there is any framework/library which provides a mechanism to test the functions which are not exported by the shared library. 我在考虑是否有任何框架/库提供了一种机制来测试共享库未导出的函数。

I want to test working of functions listed in "t" section of shared library. 我想测试共享库的“t”部分中列出的函数的工作。 For testing functions in "T" section i'hv used cppunit. 为了测试“T”部分中的功能,我使用了cppunit。

Test scenario: There is a class exported using " __ attribute __ ((visibility("default"))) " which declares a variable of another class which is having " __ attribute __ ((visibility("hidden"))) " which is defined in the same library. 测试场景:使用“ __ attribute __((visibility(”default“))) ”导出一个类,它声明另一个类的变量,该变量具有“ __ attribute __((visibility(”hidden“))) ”“在同一个库中定义。 I want to test the class with " __ attribute __ ((visibility("hidden"))) " attributes. 我想用“ __ attribute __((visibility(”hidden“))) ”属性测试该类。

Programming language of library is c++ 库的编程语言是c ++

compiler gcc 4.1.2 编译器gcc 4.1.2

platform RedHat/Solaris 平台RedHat / Solaris

As unit-tests are supposed to have intimate knowledge of the code they test, unit-tests can also employ tricks that are not available to the normal users of a piece of code. 由于单元测试应该对他们测试的代码有深入的了解,因此单元测试也可以使用一段代码的普通用户无法使用的技巧。

Possible tricks to employ here are: 这里采用的可能技巧是:

  • Ensure the unit-tests and the code-under-test are linked as a single executable, without building the code-under-test in a library first. 确保单元测试和被测代码链接为单个可执行文件,而不首先在库中构建测试代码。
  • Use a pre-processor macro to disable the visibility attributes when building for the unit-tests. 在构建单元测试时,使用预处理器宏来禁用可见性属性。

If you want to test those functions using the same binary file which you are shipping, I know just one practical solution: build some kind of maintenance hatch into your libary. 如果你想使用你运送的同一个二进制文件来测试这些函数,我只知道一个实际的解决方案:在你的库中构建一些维护孵化器。

That means, add some publicly exported functions into the lib which call the internal functions you want to test. 这意味着,将一些公开导出的函数添加到lib中,该函数调用您要测试的内部函数。 Use names for that functions making clear these are only for testing purposes, they should be not used by the "normal user" of the lib. 使用该函数的名称清除这些仅用于测试目的,lib的“普通用户”不应使用它们。 Keep the documentation of that methods internal to make it really hard for outsiders to use them. 将这些方法的文档保持在内部,使外人很难使用它们。 Add a clear warning to the official docs that, for example, all methods starting with the prefix "TEST_" are not to be used by the normal user and are subject to change with every release. 向官方文档添加一个明确的警告,例如,普通用户不会使用以前缀“TEST_”开头的所有方法,并且每个版本都会更改。

And if someone is so ignorant to use that methods either, just don't care - you cannot prevent such people from shooting themselves into the foot. 如果某人如此无知也无法使用这些方法,只是不在乎 - 你无法阻止这些人射入脚中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM