Android 用浓缩咖啡测试多项活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20427411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:51:13  来源:igfitidea点击:

Testing multiple activities with espresso

androidtestingandroid-espresso

提问by fernandohur

Is it possible to write tests across several activities using the android espresso framework?

是否可以使用 android espresso 框架跨多个活动编写测试?

采纳答案by Jigish Chawda

Yes, it is possible. In one of the samples they have demoed this here https://github.com/googlesamples/android-testing/blob/master/ui/espresso/BasicSample/app/src/androidTest/java/com/example/android/testing/espresso/BasicSample/ChangeTextBehaviorTest.java

对的,这是可能的。在他们在这里演示的示例之一中https://github.com/googlesamples/android-testing/blob/master/ui/espresso/BasicSample/app/src/androidTest/java/com/example/android/testing/浓缩咖啡/BasicSample/ChangeTextBehaviorTest.java

@Test
public void changeText_newActivity() {
    // Type text and then press the button.
    onView(withId(R.id.editTextUserInput)).perform(typeText(STRING_TO_BE_TYPED),
            closeSoftKeyboard());
    onView(withId(R.id.activityChangeTextBtn)).perform(click());

    // This view is in a different Activity, no need to tell Espresso.
    onView(withId(R.id.show_text_view)).check(matches(withText(STRING_TO_BE_TYPED)));
}

Read the inline comment.

阅读内嵌评论。

Waiting for the new activity to load is taken care of implicitly by Espresso.

等待新活动加载由 Espresso 隐式处理。

回答by ValeraZakharov

It is absolutely possible to write an Espresso (or any instrumentation based) test that crosses multiple Activities. You have to start out with one Activity, but can navigate through the UI of your application to other Activities. The only caveat - due to security restrictions, the test flow must stay within your application's process.

编写跨多个活动的 Espresso(或任何基于仪器的)测试是绝对可能的。您必须从一个 Activity 开始,但可以通过应用程序的 UI 导航到其他 Activity。唯一的警告 - 由于安全限制,测试流程必须保留在您的应用程序进程中。

回答by stef

I've tested this like:

我已经测试过这样的:

onView(withId(R.id.hello_visitor)).perform(click());
pressBack();
onView(withId(R.id.hello_visitor)).check(matches(isDisplayed())); //fails here

The click action starts a new activity, obviously.

显然,单击操作会启动一个新活动。

回答by Jing Li

Let's say you have two activities: HomeActivityand SearchResultsActivity. For the test, you want to do some actions on HomeActivity, and verify the result on SearchResultsActivity. Then the test will be written like below:

假设您有两个活动:HomeActivitySearchResultsActivity。对于测试,您希望对 HomeActivity 执行一些操作,并在 SearchResultsActivity 上验证结果。然后测试将如下编写:

public class SearchTest extends ActivityInstrumentationTestCase2<HomeActivity> {

    public SearchTest() {
        super(HomeActivity.class);
    }

    protected void setUp() throws Exception {
        super.setUp();
        getActivity(); // launch HomeActivity
    }

    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void testSearch() {
        onView(withId(R.id.edit_text_search_input)).perform(typeText("Hello World"));
        onView(withId(R.id.button_search)).perform(click());
        // at this point, another activity SearchResultsActivity is started
        onView(withId(R.id.text_view_search_result)).check(matches(withText(containsString("Hello World"))));
    }

}

So the only thing you need to care, is that you should extends the test class from ActivityInstrumentationTestCase2<FirstActivity>, and call super(FirstActivity.class) in your constructor.

所以你唯一需要关心的是,你应该从 ActivityInstrumentationTestCase2< FirstActivity>扩展测试类,并在你的构造函数中调用 super( FirstActivity.class)。

Above example is fairly easy.

上面的例子相当简单。

Advance example(when startActivityForResult happens):

高级示例(当 startActivityForResult 发生时):

Sometimes it's really confusing to write a test, where you still have two activities A and B, and the application-flow is different than above:

有时写一个测试真的很混乱,你仍然有两个活动 A 和 B,并且应用程序流与上面的不同:

  1. user does nothing on activity A, but activity A calls startActivityForResult to launch activity B;
  2. then user makes some inputs and clicks on activity B (this part is the real test);
  3. finally activity B exits, it calls setResult and resumes activity A (you need to verify result here).
  1. 用户对活动 A 不做任何事情,但活动 A 调用 startActivityForResult 来启动活动 B;
  2. 然后用户进行一些输入并点击活动B(这部分是真正的测试);
  3. 最后活动 B 退出,它调用 setResult 并恢复活动 A(您需要在此处验证结果)。

Even though the whole testing part happens on activity B, you may just need to verify one tiny piece on activity A, but your test should extend from ActivityInstrumentationTestCase2<ActivityWhoCallsStartActivityForResult> which is activity A, but not activity B. Otherwise, when test part is done, activity A won't be resumed, you have no chance to verify your result.

即使整个测试部分发生在活动 B 上,您可能只需要验证活动 A 上的一小部分,但您的测试应该从活动A,而不是活动 B 的ActivityInstrumentationTestCase2< ActivityWhoCallsStartActivityForResult>扩展。否则,当测试部分是完成,活动 A 将不会恢复,您没有机会验证您的结果。