开始测试活动 [英] Start Activity for testing

查看:72
本文介绍了开始测试活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Realm db的测验应用程序.每次用户选择答案时,都会单击一个按钮,然后出现问题"的新文本. 就这样,直到她到达我开始新活动并显示基于正确答案的分数为止.

我应该如何开始/测试(我想使用Espresso)该活动,而不必每次都全部手动输入答案,并在每个答案后单击按钮,直到找到最后一个? >

我需要的是将一些模拟数据传递给变量并进行Intent操作,但是我不知道如何并且无法在Espresso中找到与此相关的任何东西

解决方案

您可以使用以下自定义意图启动下一个活动:

@RunWith(AndroidJUnit4.class)
public class NextActivityTest {

  @Rule
  public ActivityTestRule<NextActivity> activityRule 
     = new ActivityTestRule<>(
        NextActivity.class,
        true,     // initialTouchMode
        false);   // launchActivity. False to customize the intent

  @Test
  public void intent() {
    Intent intent = new Intent();
    intent.putExtra("your_key", "your_value");

    activityRule.launchActivity(intent);

    // Continue with your test
  }
}

完整示例: https://github.com/chiuki/android-test-demo

博客文章: http://blog.sqisland.com/2015/04/espresso- 21-activitytestrule.html

I 've got a Quiz app using Realm db. Every time the user selects an answer she clicks a button and new text for Question appears. Thats it until she reaches the end where I start a new Activity and display a score based on correct answers.

How should I start/test ( with Espresso I guess ) that activity without having to enter manually every time all the answers and click the button after each answer until I reach the last one?

What I need is to pass some mock data to a variable and make an Intent but I dont know how and cant find anything related with this in Espresso

解决方案

You can launch your next activity with a custom intent like this:

@RunWith(AndroidJUnit4.class)
public class NextActivityTest {

  @Rule
  public ActivityTestRule<NextActivity> activityRule 
     = new ActivityTestRule<>(
        NextActivity.class,
        true,     // initialTouchMode
        false);   // launchActivity. False to customize the intent

  @Test
  public void intent() {
    Intent intent = new Intent();
    intent.putExtra("your_key", "your_value");

    activityRule.launchActivity(intent);

    // Continue with your test
  }
}

Full example: https://github.com/chiuki/android-test-demo

Blog post: http://blog.sqisland.com/2015/04/espresso-21-activitytestrule.html

这篇关于开始测试活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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