一个新闻 app 应该会有以下这些 activity。
语言选择 - 当用户第一次打开软件, 他必须至少选择一种语言。选择后,选项保存在共享偏好中,用户跳转到新闻列表 activity。
新闻列表 - 当用户来到新闻列表 activity,将发送一个包含语言参数的请求到服务器,并将服务器返回的内容显示在 recycler view 上(包含有新闻列表的 id, news_list)。 如果共享偏好中未存语言参数,或者服务器没有返回一个成功消息, 就会弹出一个错误对话框并且 recycler view 将不可见。如果用户只选择了一种语言,新闻列表 activity 有个 “change your language” 的按钮,或者如果用户选择多种语言,则按钮为 “change your languages” 。 (我对天发誓这是一个虚构的 app 软件)
新闻细节 - 如同名字所述, 当用户点选新闻列表项时将启动这个 activity。
这个 app 功能已经足够,,让我们深入研究下为新闻列表 activity 编写的测试用例。 这是我第一次写的代码。
<code>/*</code>
<code>click on the first news item.</code>
<code>it should open newsdetailactivity</code>
<code>*/</code>
<code>@test</code>
<code>public void testclickonanynewsitem() {</code>
<code>onview(allof(withid(r.id.news_list), isdisplayed())).perform(recyclerviewactions</code>
<code>.actiononitematposition(1, click()));</code>
<code>intended(hascomponent(newsdetailsactivity.class.getname()));</code>
<code>}</code>
<code></code>
<code>/**</code>
<code>* to test the correct text on the button</code>
<code>public void testchangelanguagefeature() {</code>
<code>int count = userpreferenceutil.getselectedlanguagescount();</code>
<code>if (count == 1) {</code>
<code>onview(withtext("choose your language")).check(matches(isdisplayed()));</code>
<code>} else if (count > 1) {</code>
<code>onview(withtext("choose your languages")).check(matches(isdisplayed()));</code>
<code>?}</code>
<a target="_blank"></a>
在第一个测试用例 <code>testclickonanynewsitem()</code>, 如果服务器没有返回成功信息,测试用例将会返回失败,因为 recycler view 是不可见的。但是这个测试用例的目的并非如此。不管该用例为 pass 还是 fail,它的最低要求是 recycler view 总是可见的, 如果因某种原因,recycler view 不可见,那么测试用例不应视为 failed。正确的测试代码应该像下面这个样子。
<code>click on any news item.</code>
<code>try {</code>
<code>/*to test this case, we need to have recyclerview present. if we don't have the</code>
<code>recyclerview present either due to the presence of error_screen, then we should consider</code>
<code>this test case successful. the test case should be unsuccesful only when we click on a</code>
<code>news item and it doesn't open newsdetail activity</code>
<code>viewinteraction viewinteraction = onview(withid(r.id.news_list));</code>
<code>viewinteraction.check(matches(isdisplayed()));</code>
<code>} catch (nomatchingviewexception e) {</code>
<code>return;</code>
<code>} catch (assertionfailederror e) {</code>
<code>&nbsp; &nbsp;//在这里我们确信,news_list的 recyclerview 对用户是可见的。</code>
<code>&nbsp; &nbsp;onview(allof(withid(r.id.news_list), isdisplayed())).perform(recyclerviewactions</code>
当我开始测试, 我通常按如下顺序测试 activity:
语言选择
新闻列表
新闻细节
因为我首先测试语言选择 activity,在测试 newslist activity 之前,总有一种语言已经是选择好了的。但是当我先测试新闻列表 activity 时,测试用例开始返回错误信息。原因很简单 - 没有选择语言,recycler view 不会显示。注意, 测试用例的执行顺序不能影响测试结果。 因此在运行测试用例之前, 语言选项必须是保存在共享偏好中的。在本例中,测试用例独立于语言选择 activity 的测试。
<code>@rule</code>
<code>public activitytestrule activitytestrule =</code>
<code>new activitytestrule(topicsactivity.class, false, false);</code>
<code>userpreferenceutil.saveuserprimarylanguage("english");</code>
<code>intent intent = new intent();</code>
<code>activitytestrule.launchactivity(intent);</code>
现在在第二个测试用例 <code>testchangelanguagefeature()</code> 中,我们获取到用户选择语言的个数,基于这个数目,我们写了 if-else 条件来进行测试。 但是 if-else 条件应该写在你的代码当中,而不是测试代码里。每一个条件应该单独测试。 因此,在本例中,不是只写一条测试用例,而是要写如下两个测试用例。
<code>* to test the correct text on the button when only one language is selected.</code>
<code>public void testchangelanguagefeatureforsingelanguage() {</code>
<code>//other initializations</code>
<code>userpreferenceutil.saveselectedlanguagescount(1);</code>
<code>* to test the correct text on the button when more than one language is selected.</code>
<code>public void testchangelanguagefeatureformultiplelanguages() {</code>
<code>userpreferenceutil.saveselectedlanguagescount(5); //write anything greater than 1.</code>
在大多数应用中,我们与外部网络或者数据库进行交互。一个测试用例运行时可以向服务器发送一个请求,并获取成功或失败的返回信息。但是不能因从服务器获取到失败信息,就认为测试用例没有通过。这样想这个问题 - 如果测试用例失败,然后我们修改客户端代码,以便测试用例通过。 但是在本例中, 我们要在客户端进行任何更改吗?- no。
但是你应该也无法完全避免要测试网络请求和响应。由于服务器是一个外部代理,我们可以设想一个场景,发送一些可能导致程序崩溃的错误响应。因此,你写的测试用例应该覆盖所有可能来自服务器的响应,甚至包括服务器决不会发出的响应。这样可以覆盖所有代码,并能保证应用可以处理所有响应,而不会崩溃。
正确的编写测试用例与编写这些测试代码同等重要。
原文发布时间为:2017-02-12
本文来自云栖社区合作伙伴“linux中国”