0

我正在使用adb测试命令一次在多个设备上运行测试。我的伪外壳脚本如下所示:Android咖啡控制台日志Build.Serial onFail

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类并覆盖这样的失败()方法,

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

比方说,这是我的样品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())); 
    } 
} 

在多个设备上运行它,我使用的是GradleconnectedAndroidTest其延伸connectedCheck,所以它:

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

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

只要去使用终端或控制台去你的项目的目录,然后使用:

./gradlew connectedAndroidTest

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

它应该是这样的:

*emphasized text*

希望这将有助于

+0

谢谢你的答复。我忘记提及,我知道通过使用gradle任务以这种方式运行测试。唯一的问题是测试不能立即运行,即gradle builds,安装apks,运行测试并卸载apk。我喜欢adb命令,因为它会立即运行测试。你知道一种跳过构建部分的方法,只需使用gradle connectedCheck命令运行测试? –

+0

我不太了解Gradle,但是尝试使用找到哪些命令是“connectedAndroidTest”或“connectedCheck”。也许'./gradlew test'会跳过建设 – piotrek1543