【发布时间】:2015-05-03 02:56:15
【问题描述】:
我在 Android Studio 中创建了简单的测试。它只打印hello from test 并将1 与5 进行比较
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);
}
}
我运行它,但在任何地方都没有看到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
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);
}
}
截图:
测试完成。
但是logcat 是空的。
【问题讨论】:
-
我之前没有在 Android 上做过单元测试(嘿,你真好!)但是在 Android Studio 中,有一个名为 Logs 的选项卡,你可以使用搜索框来过滤日志。
-
@gengkev 我检查了重复并尝试了
Log.d并尝试通过hello from test进行搜索,但没有帮助。 -
根据stackoverflow.com/q/5451614
Log.d应该可以工作... -
你试过 adb logcat 吗?