简体   繁体   English

客户端/服务器应用程序的自动化测试

[英]Automated test of client/server application

As the headline says, how would you test a client/server application, that is written in C/C++, that talks through a protocol over a network? 正如标题所说,您将如何测试用C / C ++编写的,通过网络进行通讯的客户端/服务器应用程序? Im a bit confused on how to do this. 我对如何执行此操作有些困惑。 I have thought about making some mocking, but I have never tried mocking, so I dont know if this is the best way. 我曾经考虑过进行一些模拟,但是我从未尝试过模拟,所以我不知道这是否是最好的方法。 How should I do this? 我应该怎么做? I have written many unit tests, but never tried to test something that interact over a network. 我编写了许多单元测试,但从未尝试过测试通过网络交互的内容。

I use the command pattern in the unit test driver (client) to send test commands to the server. 我使用单元测试驱动程序(客户端)中的命令模式将测试命令发送到服务器。 The advantage of this is that the test is coded in one place. 这样做的好处是将测试编码在一个地方。

Example for testing a request timeout: 测试请求超时的示例:

Client sends a sleep command to server and then the request. 客户端先向服务器发送睡眠命令,然后向服务器发送请求。 The request times out and the test case is passed. 请求超时,测试用例通过。

Typically you'll want to use mocking to verify that each side reacts as required to messages from the other side (eg, that when the client receives a response from the server that it processes that response correctly). 通常,您将需要使用模拟来验证双方是否对另一方的消息做出了必要的反应(例如,当客户端从服务器接收到响应时,它会正确处理该响应)。

To test the network functionality itself, you can test both running on the same machine, and you can run one (or both) inside a virtual machine. 要测试网络功能本身,您可以测试在同一台计算机上运行的这两种功能,也可以在虚拟机内部运行一个(或两者)的功能。 If you have two network adapters, you can even dedicate each to a virtual machine so the network traffic actually goes out one, to a switch/router, and comes back in the other (particularly useful when/if you want to capture and verify packets). 如果您有两个网络适配器,则甚至可以将每个适配器专用于一个虚拟机,这样,网络流量实际上就从一个虚拟机流出,到交换机/路由器,然后又从另一个流回(特别是在您要捕获和验证数据包时很有用) )。

I have some client/server code that I unit test through the loopback address. 我有一些通过回送地址进行单元测试的客户端/服务器代码。 I use some mocks when I have to test error conditions. 当我必须测试错误条件时,我会使用一些模拟。 So, I test with the real code when I can and the mocks when I need to trigger very specific conditions. 因此,我会尽可能地使用真实代码进行测试,而当我需要触发非常特定的条件时会进行模拟测试。

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

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