问题描述
我现在正在开发P2P聊天应用程序(适用于Android).而且,我想添加一些UI测试,以便在每次更改大部分代码后都不会手动遍历所有用例(发送/接收消息,连接请求,断开连接,丢失连接等).
I'm working on P2P chat application (for Android) right now. And I would like to add some UI tests to not go through all use-cases (sending/receiving messages, connection requests, disconnections, losing connection and so on) manually every time after I change some big part of my code.
因此,我最初的想法是为两个设备创建两组Espresso测试,一组将发送请求和消息,而另一组将作为接收器并同时运行它们.这只是一个抽象的想法,我的问题是:是否可以运行两组将相互影响的测试?即使答案是是",我还是应该实施这种方法,还是应该以某种方式模拟连接机制并仅在一个设备上运行测试?(如果这种方法很好,我该如何实施?)您能建议一种正确/更好的方法吗?
So, my initial thought was creating two sets of Espresso test for two devices, one will send requests and messages and another will be a receiver and run them simultaneously. It's just abstract idea and my question is: is that possible to run two sets of tests which will interact with each other? And even if the answer is "yes", should I implement this approach, or should I somehow mock connection mechanism and run tests only on one device? (In case this approach is fine, how can I implement that?) Can you suggest a right/better approach?
我没有找到任何类似的问题,而且我认为这可能是愚蠢的,对于拥有大量测试经验的每个人来说,答案都是显而易见的.无论如何,答案对于处于类似情况的其他人可能是有用的.
I didn't find any similar question, and I think maybe it is stupid and an answer is obvious for everybody who has much experience with testing. Anyway, the answer may be useful for some other guys in the similar situation.
谢谢.
推荐答案
好了,在这里,我问了这个问题且活动为零8个月后,我什至获得了风滚草"成就.
Ok, here we are, 8 months after I asked this question and zero activity, I have even earned 'Tumbleweed' achievement.
我不会说我已经通过测试两个设备之间的通信解决了我的问题,但是至少我已经实现了一些可行的功能,并且希望与需要它的任何人共享.
I wouldn't say I have solved my problem with testing communication between two devices but at least I have implemented something that works, and I want to share it with anyone who may need it.
自动回复
似乎没有一种简单的方法可以在两个将相互通信的设备上同时运行两个不同的测试.因此,我决定实现通过脚本工作的Autoresponder.基本上,测试会发送一条文本消息(例如"respond_me"),自动响应程序会捕获该消息,并通过另一条文本(例如"response")进行响应,然后测试是否接收到该消息.
Looks like there is no an easy way to run two different test on two devices simultaneously which will communicate with each other. So, I've decided to implement Autoresponder which works by a script. Basically, test sends a text message for example "respond_me" and autoresponder catches it and responds via another text for example "response" and test checks if the message was received.
样品已简化
我创建了"CommunicationProxy"界面
I created 'CommunicationProxy' interface
interface CommunicationProxy {
fun onMessageReceived(message: ChatMessage)
fun onMessageSent(message: ChatMessage)
}
并在我接收消息传递事件的类中调用其方法.
and invoke its methods in a class where I receive messaging events.
private var proxy: CommunicationProxy? = ...
override fun onMessageReceived(message: ChatMessage) {
proxy?.onMessageReceived(message)
messageListener?.onMessageReceived(message)
}
override fun onMessageSent(message: ChatMessage) {
proxy?.onMessageSent(message)
messageListener?.onMessageSent(message)
}
对于 debug
和 release
构建类型,此代理是一个空接口实现:
For debug
and release
build types this proxy is an empty interface implementation:
class EmptyProxy : CommunicationProxy {
override fun onMessageReceived(message: ChatMessage) {
}
override fun onMessageSent(message: ChatMessage) {
}
}
我又添加了一个构建类型 autoresponder
,并使用一些逻辑实例化 CommunicationProxy
实现:
I added one more build type autoresponder
and instantiate CommunicationProxy
implementation with some logic:
proxy = if (BuildConfig.AUTORESPONDER) AutoresponderProxy(service) else EmptyProxy()
AutoresponderProxy
:
class AutoresponderProxy(private val service: ConnectionService?) : CommunicationProxy {
companion object {
const val COMMAND_SEND_TEXT = "-send_text"
const val RESPONSE_RECEIVED = "+text_message"
}
override fun onMessageReceived(message: ChatMessage) {
service?.let {
when {
message.text == COMMAND_SEND_TEXT -> {
it.sendMessage(RESPONSE_RECEIVED)
}
...
}
}
}
override fun onMessageSent(message: ChatMessage) {
}
}
我在一台设备上安装了 autoresponder
,另一台设备运行了Espresso测试,该测试将'-send_text'文本键入EditText并按下'Send'按钮,然后等待RecyclerView中出现'+ text_message'文本.
I install autoresponder
on one device and another one runs Espresso test which types '-send_text' text into EditText and presses 'Send' button, and waits when '+text_message' text appears in RecyclerView.
空闲资源?
此示例非常简化,并且我的 CommunicationProxy
接口具有25种方法,我尝试集成IdlingResources,但无法管理所有这些回调.我使用 Thread.sleep(...)
等待响应.
This sample is extremely simplified, and my CommunicationProxy
interface has 25 methods, I tried to integrate IdlingResources but I couldn't manage all those callbacks. I use Thread.sleep(...)
to wait for a response.
易碎
我的测试运行良好,但我知道它可能会很不稳定,无论如何,我实施了这种方法来简化我的生活,并且每次都不手动进行回归测试,我也不打算在CI或类似的工具上运行它
My test runs fine but I understand how flaky it may be, anyway I implemented this approach to simplify my life and don't do regression testing every time manually, I don't plan to run it on CI or something like this.
这篇关于测试两个设备之间的间隔(通过Espresso)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!