简体   繁体   中英

Android espresso console log Build.Serial onFail

I am running test on multiple devices at once using the adb test command. My pseudo shell script looks like this:

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

The problem is when a test fails, I have no information on which device the failure occurred. I could use LogCat but it requires looking up logcat for each device. And also, System.out.println() does not work.

One possible solution I am trying right now is by extending TestWatcher class and overriding the failed() method like this,

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);
    }
}

Implementation:

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

I cannot get the device serial yet on the terminal.

My expected output would be something like this:

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

Let's say this is my sample Espresso test:

@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()));
    }
}

To run it on multiple devices I'm using Gradle 's connectedAndroidTest which extends connectedCheck , so it :

will run on all connected devices in parallel.

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

Just go use your terminal or console to go to your project's directory, then use:

./gradlew connectedAndroidTest .

This one is very useful as it would generate HTML test output which allows you to check which method on which device had failed.

It would look like this:

*强调文字*

Hope it will help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM