简体   繁体   English

如何测试公共界面中没有的功能?

[英]How do I test features that are not in the public interface?

I'm designing a library for myself that allows the chaining of streams of data. 我正在为自己设计一个允许链接数据流的库。 Let me paint the scenario: 让我描绘一下场景:

I create a SerialDatastream which is the bottom layer and reads from and writes to a COM port. 我创建了一个SerialDatastream ,它是最底层,并从COM端口读取和写入。

I pass a pointer to this to the constructor of a ProtocolDatastream , which interprets the bytes when read from the serial datastream (although it only has knowledge that it fulfills my Datastream interface), and returns them as protocol data units. 我将指向它的指针传递给ProtocolDatastream的构造函数,该协议从串行数据流读取时解释字节(尽管它仅知道它满足我的Datastream接口),然后将它们作为协议数据单元返回。

Now let's say I want to take the information from the serial port and also log it byte for byte. 现在让我们说我想从串行端口获取信息,并逐字节记录它。 I insert a TeeDatastream in the middle which reads from one source, but outputs to two destinations: 我在中间插入一个TeeDatastream ,它从一个源读取,但输出到两个目标:

+-----> Log
               |
Serial ----> Tee ----> Protocol

TeeDatastream is implemented in the following way: when a read operation is performed from one branch, it buffers the data into a member variable. TeeDatastream通过以下方式实现:从一个分支执行读取操作时,它将数据缓冲到成员变量中。 Then, when a read operation is performed on the other branch, it reads the already-buffered data. 然后,当在另一个分支上执行读取操作时,它将读取已缓冲的数据。

(this works fine, by the way) (顺便说一句,这很好用)

What this means is that, after each operation, the class must check to see if there exists data that has been read from both branches. 这意味着在每次操作之后,该类必须检查以查看是否存在从两个分支读取的数据。 This data can then be discarded and so the buffer shrinks as well as growing. 然后可以丢弃此数据,从而使缓冲区缩小并增加。 However , this is completely invisible to any client of the class. 但是 ,这对于该类的任何客户端都是完全不可见的。 So my question is: what pattern should be used to test invisible but necessary parts of an implementation? 所以我的问题是:应该使用哪种模式来测试实现中不可见但必要的部分?

What you need are unit tests for Tee itself; 您需要的是针对Tee本身的单元测试; independent of its use later in other unit tests as part of the plumbing. 不依赖于以后在其他单元测试中用作管道的一部分。

For these new unit tests, the "invisible part" is actually what they must cover. 对于这些新的单元测试,“隐形部分”实际上是它们必须涵盖的内容。 This is no longer a hidden feature but part of the API of Tee . 这不再是隐藏的功能,而是Tee API的一部分。

Later, when you're sure that Tee works correctly (and have the necessary tests to make sure it stays that way), you can use it and be oblivious to the fact how it works. 稍后,当您确定Tee正常工作(并进行了必要的测试以确保它保持这种状态)时,您可以使用它,而不必担心它的工作原理。

In the unit test cpp file locally declare a friend class and declare each protected member function you want to test. 在单元测试cpp文件中,在本地声明一个朋友类,并声明要测试的每个受保护的成员函数。

class BlahTestable : public Blah
{
public: 
    using Blah::protectedfunction1;
    using Blah::protectedfunction2;
    etc....
};

Then in the unit test do 然后在单元测试中做

// for public members
TEST_F(BlahTest, publicfunction) {
    Blah s;
    s.publicfunction();
}

// for protected members
TEST_F(BlahTest, protectedfunction1) {
    BlahTestable s;
    s.protectedfunction1();
}

Unless I am missing something, it seems that you are looking for plain unit testing. 除非我缺少任何内容,否则似乎您正在寻找简单的单元测试。 This type of testing allows you to test both the public and the private parts of your code. 这种测试允许您同时测试代码的公共部分和私有部分。

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

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