如何在命令行中使用 Android gradlew 显示多行断言消息?

问题描述

我使用 Android Studio 创建了一个新的 Empty Activity Android 项目。我在 useAppContext() 类中的 ExampleInstrumentedTest 测试中添加了额外的一行,以显示多行断言消息:

@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        assertEquals("com.example.myapplication",appContext.getPackageName());
        
        Assert.fail("1. Test Failed\n2. Test Failed\n3. Test Failed\n");
    }
}

如果我在 Android Studio 中运行测试,我会得到正确的断言消息:

java.lang.AssertionError: 1. Test Failed
2. Test Failed
3. Test Failed

但是如果我从命令行运行它,我只会得到两行消息:

$ ./gradlew connectedDebugAndroidTest
com.example.myapplication.ExampleInstrumentedTest > useAppContext[SM-G950U - 9] Failed
        java.lang.AssertionError: 1. Test Failed
        2. Test Failed

> Task :app:connectedDebugAndroidTest Failed

平台/版本/依赖项:

MacOS 11.3.1
Android Studio 4.2.1
classpath "com.android.tools.build:gradle:4.2.1"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

如何在命令行中显示所有行?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)