iOS - 如何区分 UI 和单元测试,以及如何在这种具体情况下使用它们? [英] iOS - How to differentiate UI and Unit tests, and how to use them in this concrete situation?

查看:20
本文介绍了iOS - 如何区分 UI 和单元测试,以及如何在这种具体情况下使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 UI 和单元测试完全是初学者,并且对如何使用它们以及如何区分它们感到非常困惑.所以我想到了以下用例:

I am completely beginner about UI and unit tests, and very confused about how to use them, as well as how to differentiate them. So I have thought about the following use case:

我有一个用户拥有一组 MyData,一个像这样的简单对象:

I have a user who has a set of MyData, a simple object like this:

class MyData: NSObject {
    var settled: Bool?
    var id: String?
}

class User {
    var datas: Set<MyData>?
}

这是应用程序的行为:用户首先进入处于 loading 状态的 MyDataListViewControllerviewDidLoad 方法触发一个异步函数,假设我们为此使用 Parse:

Here is the behaviour of the application: the user comes on a MyDataListViewController that's in a loading state first, the viewDidLoad method fires an asynchronous function, let's say we use Parse for this:

static func loadDatas(success: ((Set<Data>) -> Void)?, error: ((String) -> Void)?) {
    // Here we fetch the datas and return then in the success or an error
    // ... 
}

回调结果填充控制器的UITableView,当点击一个单元格时,它会为特定的MyData打开一个模态弹窗,用一个按钮来改变它的settle 值.

The callback result fills the controller's UITableView, and when tapping a cell, it opens a modal popup for the specific MyData with a button to change its settled value.

myData.settled = !myData.settled
myData.save()

鉴于此信息,

  • 如何分离 UI 和单元测试?
  • 对于这些测试中的每一个,我应该如何设置以及应该设置什么?

感谢您阅读并帮助我,如果这听起来很初学者,再次抱歉.我对这个主题仍然很困惑.另外,抱歉,如果有比 SO 更好的地方可以解决此类问题,我真的不知道该问哪里.

Thanks for reading me and helping me out, and sorry again if this sounds very beginner. I'm still very confused about the subject. Also, sorry if a better place than SO matches for this kind of question, I really didn't know where to ask.

推荐答案

您的具体问题的答案太长而无法提供 SO 答案.但是,您的一个根本问题似乎是单元测试和 UI 测试之间的区别是什么.可以在这里回答.

The answers to your specific questions would be far too long to put in an SO answer. However, one of your root questions seems to be what is the difference between a unit test and a UI test. That can be answered here.

真正简短的版本是单元测试可以访问您应用中的代码(或您正在构建的任何类型的模块),而 UI 测试不能访问代码.

The really short version is that unit tests have access to the code in your app (or whatever kind of module you are building) and UI tests do not have access to the code.

单元测试每个测试只测试一个类.该类需要与之交互的任何对象都应替换为测试控制的假对象.如果你不插入假对象,你就无法知道失败的测试是来自你真正尝试测试的东西还是它使用的东西.(应该注意的是,还有一种集成测试之类的东西,它可以测试功能而不伪造任何网络响应).

A unit test only tests one single class per test. Any objects that the class would need to interact with should be replaced with a fake object that the test controls. If you don't insert fake objects you have no way of knowing if a failed test is from the thing you were really trying test or something that it uses. (It should be noted that there is also such a thing as integration tests that test features without faking anything more than maybe network responses).

理想情况下,对于单元测试,您希望应用中的每个类都有一个测试用例类.然后每个测试用例可以专注于单个类并尽可能地对其进行测试.

Ideally for unit tests you want to have a test case class for each class in your app. Then each test case can focus on a single class and test it as throughly as possible.

UI 测试利用 Apple 各种平台的辅助功能来有效地假装是使用该应用的用户.这个想法是你使用 Xcode 来记录一系列操作来完成你的应用程序中的任务(也许是添加一个待办事项),然后编辑 Xcode 为你生成的代码来测试特定的东西,就像表格视图现在有一个一样其中的待办事项以及该单元格是否包含正确的标题.

UI Tests make use of the accessibility features of Apple's various platforms to effectively pretend to be a user using the app. The idea is you use Xcode to record the series of actions to accomplish a task within your app (perhaps to add a todo item) and then edit the code Xcode generates for you to test for specific things, like does the table view now have one todo item in it and does that cell contain the right title.

您将需要使用 Xcode 的 UI 测试记录功能开始,因为代码起初对您来说看起来有点陌生.这是因为 UI 测试无权访问应用程序的代码,因此它无法要求出口,它必须找到具有可访问性的元素(如果您为尽可能多的应用程序添加可访问性标签,您的生活会更轻松)尽可能使用 UI 元素).

You will need to use Xcode's recording feature for UI Tests to start with as the code will look a bit foreign to you at first. This is because the UI Tests don't have access to the app's code so it can't ask for an outlet, it has to find the element with accessibility (your life will be way easier if you add accessibility labels to as many of your UI elements as possible).

为了更深入地了解使用 Xcode 进行单元和 UI 测试,这里有一些博客文章和书籍:

To get a more in depth look at Unit and UI Testing with Xcode there are a few blog posts and books around:

博文

书籍

  • https://www.packtpub.com/application-development/test-driven-ios-development-swift-4-third-edition (available on Safari online if you have an account)
  • https://www.hackingwithswift.com/store/testing-swift (preorders only at the moment)

我确定还有其他资源,但这些应该可以帮助您入门.Ray Wenderlich 的文章应该让您有一个很好的概述,并让您可以为 Google 和 SO 制定更有针对性的问题.

I'm sure there are other resources out there but these should get you started. The Ray Wenderlich article should get you a good overview and allow you to formulate more targeted questions for Google and SO.

这篇关于iOS - 如何区分 UI 和单元测试,以及如何在这种具体情况下使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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