简体   繁体   English

如何在TDD中编写异步方法测试?

[英]How to write test of async methods in TDD?

I need to write unit test of an asynchronous method. 我需要编写一个异步方法的单元测试。

In my case I have client(simply tcp client). 就我而言,我有客户端(仅是tcp客户端)。 It send message to server(tcp server). 它发送消息到服务器(TCP服务器)。 Server takes this message asynchronously. 服务器异步接收此消息。

I want to get the message the server received and compare the message that client had sent. 我想获取服务器收到的消息,并比较客户端已发送的消息。

How can I do this? 我怎样才能做到这一点?

Mainly, I want to learn How to apply TDD to asynchronous methods. 我主要想学习如何将TDD应用于异步方法。

Any help will be greately appreciated. 任何帮助将不胜感激。

If you are attempting to actually send data over a TCP socket between a client and server, this is not a unit test. 如果您试图通过客户端和服务器之间的TCP套接字实际发送数据,则这不是单元测试。 Unit tests would be when you mock the client to test the server and the mock the server to test the client. 单元测试将是在模拟客户端以测试服务器而模拟服务器以测试客户端时。 You don't need to unit test the TCP protocol not the library that you are using to send the TCP (if you are using a library that has testing). 您不需要对TCP协议进行单元测试,而无需对用来发送TCP的库进行单元测试(如果您使用的是经过测试的库)。

Using an actual client, server and TCP socket would be an integration test. 使用实际的客户端,服务器和TCP套接字将是一个集成测试。

As far as writing junit for asynchronous method is concerned, there won't be any difference technically. 就为异步方法编写junit而言,技术上没有任何区别。 I mean you don't have to do technically different here. 我的意思是您不必在技术上有所不同。 You just need to verify the output independent of synchronous method. 您只需要验证输出与同步方法无关即可。 Say a different asynch thread is created for getting image from server, you just need to verify your are getting output without any dependency on any other thread. 假设创建了一个不同的异步线程来从服务器获取图像,则只需验证您正在获取的输出而不会依赖于任何其他线程。

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

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