使用 Swift 在 Xcode 中进行异步 UI 测试 [英] Asynchronous UI Testing in Xcode With Swift

查看:27
本文介绍了使用 Swift 在 Xcode 中进行异步 UI 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个发出大量网络请求的应用程序.像往常一样,他们是异步,即请求方法的调用立即返回,结果在一些延迟后通过委托方法或在闭包中传递.现在在我的注册屏幕上,我向我的后端发送了一个注册请求,然后想要验证请求完成时是否显示成功 UI.

I am writing an app that makes plenty of network requests. As usual they are async, i.e. the call of the request method returns immediately and the result is delivered via a delegate method or in a closure after some delay. Now on my registration screen I sent a register request to my backend and want to verify that the success UI is shown when the request finishes.

有哪些选项可以等待请求完成,请验证成功 UI 之后才离开测试方法?

Which options are out there to wait for the request to finish, verify the success UI and only after that leave the test method?

还有比等待请求完成更聪明的选择吗?

Also are there any more clever options than waiting for the request to finish?

提前致谢!

推荐答案

Trivial Approach

Apple 在 Xcode 9/iOS 11 中实现了重大改进,使您能够等待 UI 元素的出现.您可以使用以下单行:

Apple implemented major improvements in Xcode 9 / iOS 11 that enables you to wait for the appearance of a UI element. You can use the following one-liner:

<#yourElement#>.waitForExistence(timeout: 5)

高级方法

一般来说,UI 和单元测试(此处称为测试)必须尽可能快地运行,以便开发人员可以经常运行它们,并且不会因每天多次运行缓慢的测试套件而感到沮丧.在某些情况下,(内部或与安全相关的)应用程序可能会访问只能从某些网络/IP 范围/主机访问的 API.此外,大多数 CI 服务提供非常糟糕的硬件和有限的互联网连接速度.

In general UI and unit tests (referred to as tests here) must run as fast as possible so the developer can run them often and does not get frustrated by the need to run a slow test suite multiple times a day. In some cases, there is the possibility that an (internal or security-related) app accesses an API that can only be accessed from certain networks / IP ranges / hosts. Also, most CI services offer pretty bad hardware and limited internet-connection speed.

出于所有这些原因,建议以不执行实际网络请求的方式实施测试.相反,它们使用虚假数据运行,即所谓的装置.聪明的开发人员以一种可以使用简单开关(如布尔属性)切换数据源的方式来实现此测试套件.此外,当开关设置为获取真实的后端数据时,灯具可以从后端自动刷新/记录.这样就可以很容易地更新虚假数据并快速检测 API 的变化.

For all of those reasons, it is recommended to implement tests in a way that they do no real network requests. Instead, they are run with fake data, so-called fixtures. A clever developer realizes this test suite in a way that source of the data can be switched using a simple switch like a boolean property. Additionally, when the switch is set to fetch real backend data the fixtures can be refreshed/recorded from the backend automatically. This way it is pretty easy to update the fake data and quickly detect changes of the API.

但这种方法的主要优点是速度.您的测试不会发出真正的网络请求,而是针对使它们独立的本地数据运行:

But the main advantage of this approach is speed. Your test will not make real network requests but instead run against local data what makes them independent on:

  • 服务器问题
  • 连接速度
  • 网络限制

通过这种方式,您可以非常快速地运行测试,从而可以更频繁地运行 - 这是编写代码的好方法(测试驱动开发").

This way you can run your tests very fast and thus much more often - which is a good way of writing code ("Test Driven Development").

另一方面,您将不再立即检测到服务器更改,因为当后端数据更改时,假数据不会更改.但这可以通过简单地使用您实现的开关刷新您的装置来解决,因为您是一个聪明的开发人员,可以让这个问题成为您可以告诉您的孩子的故事!

On the other hand, you won't detect server changes immediately anymore since the fake data won't change when the backend data changes. But this is solved by simply refreshing your fixtures using the switch you have implemented because you are a smart developer which makes this issue a story you can tell your children!

但是等等,我忘记了一些东西!为什么这是上述微不足道的方法的替代品 - 你问?简单的!由于您使用立即可用的本地数据,因此您也可以立即调用完成处理程序.因此在执行请求和验证您的成功 UI 之间没有延迟.这意味着您无需等待,这让您的测试更快!

But wait, I forgot something! Why this is a replacement for the trivial approach above - you ask? Simple! Since you use local data which is available immediately you also can call the completion handler immediately too. So there is no delay between doing the request and verifying your success UI. This means you don't need to wait which makes your tests even faster!

我希望这可以帮助我的一些伙伴.如果您需要有关此主题的更多指导,请不要犹豫并回复此帖子.

I hope this helps some of my fellows out there. If you need more guidance regarding this topic don't hesitate and reply to this post.

青!

这篇关于使用 Swift 在 Xcode 中进行异步 UI 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆