gpt4 book ai didi

java - android 测试中的 System.out.println

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:36:58 25 4
gpt4 key购买 nike

我在 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);
}
}

我运行它但在任何地方都看不到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);
}
}

截图:

测试完成。 Running test

但是 logcat 是空的。 empty logcat

最佳答案

使用 Log 打印字符串并在 logcat 中检查它们。

您可以根据需要使用不同的变体,例如 Log.dLog.iLog.e 等用于调试、信息、错误等不同目的。

并确保为您的 logcat 选择了正确的“日志级别”。如果它在 Error 上,您将看不到 Log.d 的输出。

Further read

enter image description here enter image description here

关于java - android 测试中的 System.out.println,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30009768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com