【问题标题】:Testing using Espresso使用 Espresso 进行测试
【发布时间】:2017-04-08 00:28:06
【问题描述】:

如何使用 espresso 为以下代码编写测试用例。我有以下代码,当单击图标时执行。 我知道我可以使用tented(toPackage(....

try {
 intent = new Intent(Intent.ACTION_DIAL);
 intent.setData(Uri.parse("tel:" +"xxxxxx"); // 12 digit mobile no
    if (intent.resolveActivity(context.getPackageManager()) != null) {
                        startActivity(intent)
      }
    }
catch (Exception e) {
    Toast.makeText(getActivity(), "No phone number available", Toast.LENGTH_SHORT).show();
                }

【问题讨论】:

    标签: android testing android-espresso


    【解决方案1】:

    答案是在您的测试代码之后使用“预期”方法来验证活动结果是否符合您的要求。看起来像这样:

    @Test
    public void typeNumber_ValidInput_InitiatesCall() {
        // Types a phone number into the dialer edit text field and presses the call button.
        onView(withId(R.id.edit_text_caller_number))
                .perform(typeText(VALID_PHONE_NUMBER), closeSoftKeyboard());
        onView(withId(R.id.button_call_number)).perform(click());
    
        // Verify that an intent to the dialer was sent with the correct action, phone
        // number and package. Think of Intents intended API as the equivalent to Mockito's verify.
        intended(allOf(
                hasAction(Intent.ACTION_CALL),
                hasData(INTENT_DATA_PHONE_NUMBER),
                toPackage(PACKAGE_ANDROID_DIALER)));
    }
    

    但是,作为全自动测试的一部分,您还需要对活动的响应进行存根处理,以便它可以在不需要实际阻塞用户输入的情况下运行。您需要在运行测试之前设置 Intent 存根:

    @Before
        public void stubAllExternalIntents() {
            // By default Espresso Intents does not stub any Intents. Stubbing needs to be setup before
            // every test run. In this case all external Intents will be blocked.
            intending(not(isInternal())).respondWith(new ActivityResult(Activity.RESULT_OK, null));
        }
    

    那么你可以像这样编写测试的对应部分:

    @Test
        public void pickContactButton_click_SelectsPhoneNumber() {
            // Stub all Intents to ContactsActivity to return VALID_PHONE_NUMBER. Note that the Activity
            // is never launched and result is stubbed.
            intending(hasComponent(hasShortClassName(".ContactsActivity")))
                    .respondWith(new ActivityResult(Activity.RESULT_OK,
                            ContactsActivity.createResultData(VALID_PHONE_NUMBER)));
    
            // Click the pick contact button.
            onView(withId(R.id.button_pick_contact)).perform(click());
    
            // Check that the number is displayed in the UI.
            onView(withId(R.id.edit_text_caller_number))
                    .check(matches(withText(VALID_PHONE_NUMBER)));
        }
    

    如果您需要使用来自其他应用程序(如电话拨号器)的实际用户输入进行验证,这超出了 Espresso 的范围。由于我目前与帮助处理此类案例的供应商合作,因此我不愿说出名称和工具,但很多人确实需要编写模拟真实端到端体验的测试。

    Mike Evans 有一篇很棒的关于测试意图的文章here,而且总是有 android 文档here

    【讨论】:

    • 非常感谢您的回答,但我正在寻找解决问题的方法,即我们使用 startActivity 而不是 startActivityForResult 启动拨号器活动。我使用了与您提到的类似的策略来模拟相机意图
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多