android测试中的System.out.println

Max*_*mov 9 java android

我在Android Studio中创建了简单的测试.它只是打印hello from test并比较15

package com.example.maks.firstapp.test;

import android.test.InstrumentationTestCase;

public class ExampleTest extends InstrumentationTestCase {
    public void test() throws Exception {
        System.out.println("hello from test");

        final int expected = 1;
        final int reality = 5;
        assertEquals(expected, reality);
    }
}
Run Code Online (Sandbox Code Playgroud)

我跑了,但没有看到hello from test任何地方.

输出:

Running tests
Test running started
junit.framework.AssertionFailedError: expected:<1> but was:<5>
at com.example.maks.firstapp.test.ExampleTest.test(ExampleTest.java:15)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)

Finish
Run Code Online (Sandbox Code Playgroud)

System.out.println不实际打印

更新

我尝试使用Log.d("MyApp", "hello from test");但结果是一样的.

我尝试在不同的子窗口中搜索hello from test但没有找到任何内容.

更新2

我更改了代码使用e.Log:

package com.example.maks.firstapp.test;

import android.test.InstrumentationTestCase;
import android.util.Log;

public class ExampleTest extends InstrumentationTestCase {
    public void test() throws Exception {
        Log.e("MyApp", "I am here");

        final int expected = 1;
        final int reality = 5;
        assertEquals(expected, reality);
    }
}
Run Code Online (Sandbox Code Playgroud)

截图:

测试完成. 运行测试

但是logcat空的. 空的logcat

Sam*_*rai 6

使用Log打印出字符串,并检查他们在你的logcat.

有不同的变体,你可以在你需要像基地Log.d,Log.i,Log.e,等,这是针对不同的目的,如调试,信息,错误等

并确保选择适当的"日志级别" logcat.如果它打开Error你将看不到输出Log.d.

进一步阅读

在此输入图像描述 在此输入图像描述