Java android测试中的System.out.println
我已经在Android Studio中创建了简单的测试。它只是打印测试中的Java android测试中的System.out.println,java,android,Java,Android,我已经在Android Studio中创建了简单的测试。它只是打印测试中的hello,并将1与5 package com.example.maks.firstapp.test; import android.test.InstrumentationTestCase; public class ExampleTest extends InstrumentationTestCase { public void test() throws Exception { Syste
hello
,并将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
输出:
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
截图:
测试完成。
但是logcat
为空。
使用
Log
打印字符串并在logcat
中检查它们
您可以根据需要使用不同的变体,如Log.d
、Log.i
、Log.e
,用于调试、信息、错误等不同目的
并确保为您的logcat
选择了正确的“日志级别”。如果启用了Error
,则不会看到Log.d
的输出
当类位于错误的测试文件夹中时,会发生此问题!,更改到您拥有的文件夹(测试)。
你的类可能在文件夹中(AndroidTest)我以前没有在Android上做过单元测试(嘿,太好了!),但是在Android Studio中,有一个叫做Logs之类的标签,您可以使用搜索框来过滤日志。@耿克夫我签出了副本并尝试了
Log.d
并尝试了通过hello from test
进行搜索,但没有任何帮助。根据Log.d
应该可以工作…尝试了吗?您尝试过adb logcat吗?我尝试了Log.e
所以对于任何日志级别
我都必须看到我的消息,但我看不到,如果这是问题所在,那么我只能想象代码的一部分从未达到(或者构建失败或其他)。如果应用程序运行,则只需在setContentView
之后的第一个活动中尝试Log.e
,以进行测试。
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);
}
}