如何编写一个在运行时确定视图内容的Android UI测试? [英] How to write an Android UI test where the contents of views are determined at runtime?

查看:86
本文介绍了如何编写一个在运行时确定视图内容的Android UI测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个时间表查看应用程序,要实现的第一个功能是选择要查看时间表的课程.

I'm writing a timetable-viewing application and the first feature to be implemented is choosing the course for which to view the timetable.

用户从列表中选择课程名称,然后转到另一个屏幕以进一步指定他们当前所处课程的年份,组等.用于选择课程和编辑课程详细信息的屏幕是如下:

The user chooses the name of their course from a list and is taken to another screen to further specify which year, group etc. of the course they're currently in. The screens to choose your course and edit your course details are as follows:

受到 Google I/O 17演讲的启发在Android上的测试驱动开发"中,我希望编写一个用于测试该功能的UI测试用例.具体来说,我希望测试确认用户单击课程名称之一后,他们将被带到编辑课程详细信息"屏幕,并且课程名称位于顶部(例如). 会计和财务"与列表中单击的内容匹配.

Inspired by the Google I/O 17 talk on Test-Driven Development on Android, what I wish to write is a UI test case to test this feature. Specifically, I want my test to confirm that if the user clicks on one of the course names they will be taken to the 'edit course details' screen and that the course title at the top e.g. 'Accounting and Finance' matches the one that was clicked in the list.

使用 Espresso 进行测试选择标题为会计与金融"的课程并查看下一个屏幕上是否显示正确的标题的特定情况,看起来(大致)如下:

The test, using Espresso, for the specific case of choosing the course titled 'Accounting and Finance' and seeing if the correct title shows up on the next screen, would look (roughly) like this:

@Test
public void chooseCourse() {
    onView(withId(R.id.rv_course_list))
            .perform(RecyclerViewActions.actionOnItemAtPosition(
                    /*Somehow find position of Accounting and Finance*/,
                    click())
            );


    onView(withId(R.id.course_title))
            .check(matches(withText("Accounting and Finance")))
            .check(matches(isDisplayed()));
}

我的问题是RecyclerView课程列表将在运行时使用HTTP请求或SQLite数据库的结果填充,并且无法事先知道该列表是否包含会计和财务"或其他内容(我不希望测试失败是因为应用程序运行时列表中没有列出特定课程.

My problem is that the RecyclerView course list will be populated using results from a HTTP request or a SQLite database at runtime and there is no way of knowing beforehand whether the list will contain 'Accounting and Finance' or something else (I don't want the test to fail just because a specific course isn't in the list when the application runs).

考虑到这一点,我无法将课程名称硬编码到测试中.请注意,我不是要编写单元测试,在这里我只是模拟获取课程列表的依赖关系,以确保会计和财务"在列表中(并且可能只是隔离) ChooseCourseActivity类,捕获其Intent).

Taking this into consideration I cannot hard-code the course name into the test. Please note that I'm not trying to write a unit test where I would just mock the dependency of obtaining the course list to ensure that 'Accounting and Finance' is in the list (and would probably just isolate the ChooseCourseActivity class, capturing its Intents).

在我上面链接的视频中,演示者通过注释应用程序的添加注释功能的UI测试与我们交谈,该测试如下:

In the video I linked to above, the presenter talks us through a UI test of the add-note feature of a notes application where the test is as follows:

  1. 点击添加注释"按钮
  2. 输入注释的标题和描述
  3. 点击保存笔记"按钮
  4. 验证列表中是否包含包含第2步详细信息的新笔记

在这种情况下,对用于查找视图的文本(标题和描述)进行硬编码非常有用,因为测试中的代码确定,即视图列表中的注释,即注释的内容.就我而言,视图的内容将由HTTP响应或数据库查询确定

In that scenario hard-coding the text (title and description) used to find the views works great because the code in the test determines the contents of the view i.e. the note in the notes list. In my case, the contents of the view will be determined by a HTTP response or db query

  • 在实施之前,如何为选择课程功能编写适当的UI测试?
  • 视频中用于测试笔记应用程序的方法是否根本不适用于我的功能(因为我无法对要从列表中选择的课程名称进行硬编码)?
  • 我只是误解了UI测试,是否应该嘲笑依赖项(即使视频中的发言人没有)?

编辑:解决此问题的最佳方法似乎是所谓的密封测试"和

Edit: The best solution to this problem seems to be what is called 'hermetic testing' and this blog describes how to apply it to android UI.

推荐答案

您可以通过几种不同的方式对此进行测试,而我在我的应用程序中使用了所有这些的组合.

You can test this in a few different ways and I've used a combination of all of them within my apps.

1)您可以按照建议模拟网络/数据库层.

1) You can mock your network/database layer as you suggested.

2)您可以在测试中调用API,以获取预期"数据集,然后验证其是否正确显示在用户界面中.

2) You can make a call to your API in your test to get your 'expected' data set and then verify that it appears correctly in your UI.

3)您可以在网络层中存根,以允许您注入已知数据而不依赖于网络终结点.尤其是通过翻新,可以很容易地做到这一点.

3) You can Stub your networking layer to allow you to inject known data and not rely on your networking endpoint. Retrofit in particular makes it very easy to do this.

4)您可以使用不会频繁更改的已知测试数据集针对实时端点进行测试.

4) You can test against live endpoints using a known test dataset that won't change frequently.

我倾向于将Espresso用于UI级别测试和全面的回归测试.在UI级别测试中,我对网络/设备持久性层进行了存根,仅使用在测试期间注入的数据集来测试客户端.对于回归测试,我关心的是测试与我的API的集成,对于那些我使用已知数据不变的测试帐户的人.

I tend to use Espresso both for UI level tests and full blown regression tests. In the UI level tests, I stub out the networking/on device persistence layer and just test the client side with datasets that I inject during the test. For regression tests, I care about testing the integration with my API and for those I use a testing account that has known unchanging data.

这篇关于如何编写一个在运行时确定视图内容的Android UI测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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