Android espresso控制台日志Build.Serial onFail

时间:2016-09-19 21:33:20

标签: android android-studio android-testing android-espresso

我正在使用adb test命令一次在多个设备上运行测试。我的伪shell脚本如下所示:

for each device
   adb -s ${device} shell am instrument -w -e ${classOrPkg} ${androidTestPackage}${test_name} ${main_package}.${flavor}.test/android.support.test.runner.AndroidJUnitRunner &

问题是当测试失败时,我没有关于哪个设备发生故障的信息。我可以使用LogCat,但它需要为每个设备查找logcat。而且,System.out.println()不起作用。

我现在正在尝试的一个可能的解决方案是扩展TestWatcher类并覆盖fail()方法,如下所示,

public class TestWatcherRule extends TestWatcher {
    @Override
    protected void failed(Throwable e, Description description) {
        Description d = Description.createTestDescription(e.getClass(), "<<<< Failed on Device: " + Build.SERIAL);
        super.failed(e, d);
    }
}

实现:

@Rule
public TestWatcher testWatcher = new TestWatcherRule();
assertThat("My message", true, is(false));

我无法在终端上获得设备序列。

我的预期输出是这样的:

com.myapp.mobile.tests.BenefitCardDBTest:
Error in addDeleteCard(com.myapp.mobile.tests.BenefitCardDBTest):
**<<<< Failed on Device: HTC10xwrtxe**
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: com.myapp.mobile.qa:id/drawer_layout

1 个答案:

答案 0 :(得分:1)

假设这是我的样本Espresso测试:

@RunWith(AndroidJUnit4.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SettingsActivityTest {

    @Rule
    public ActivityTestRule<SettingsActivity> mRule = new ActivityTestRule<>(SettingsActivity.class);

    @Test
    public void checkIfToolbarIsProperlyDisplayed() throws InterruptedException {
        onView(withText(R.string.action_settings)).check(matches(withParent(withId(R.id.toolbar))));
        onView(withId(R.id.toolbar)).check(matches(isDisplayed()));
    }
}

要在多个设备上运行,我正在使用扩展Gradle的{​​{1}} connectedAndroidTest,所以:

  

将在所有连接的设备上并行运行。

     

自:   http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Android-tasks

只需使用终端或控制台转到项目目录,然后使用:

connectedCheck

这个非常有用,因为它会生成HTML测试输出,允许您检查哪个设备失败的方法。

看起来像这样:

*emphasized text*

希望它会有所帮助