【问题标题】:System.out.println in android testandroid 测试中的 System.out.println
【发布时间】:2015-05-03 02:56:15
【问题描述】:

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

截图:

测试完成。

但是logcat 是空的。

【问题讨论】:

  • 我之前没有在 Android 上做过单元测试(嘿,你真好!)但是在 Android Studio 中,有一个名为 Logs 的选项卡,你可以使用搜索框来过滤日志。
  • @gengkev 我检查了重复并尝试了Log.d 并尝试通过hello from test 进行搜索,但没有帮助。
  • 根据stackoverflow.com/q/5451614 Log.d 应该可以工作...
  • 你试过 adb logcat 吗?

标签: java android


【解决方案1】:

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

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

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

Further read

【讨论】:

  • 我尝试了Log.e,所以对于任何Log level,我必须看到我的消息,但我看不到它。
  • 如果这是问题所在,那么我只能想象这部分代码从未达到(或构建失败或其他什么)。如果应用程序运行,那么只需在 setContentView 之后的第一个活动中尝试 Log.e 即可进行测试。
  • 如果 Android Studio 抛出“找不到符号变量日志”,不要忘记在类的顶部添加 import android.util.Log;
【解决方案2】:

当类在错误的测试文件夹中时会发生此问题!,更改为您拥有的文件夹(测试)。 您的课程可能在文件夹中 (AndroidTest)

【讨论】:

  • 因为是instrumentation测试用例,所以应该只在androidTest文件夹下。
猜你喜欢
  • 2010-11-10
  • 1970-01-01
  • 1970-01-01
  • 2012-05-20
  • 2011-08-15
  • 2018-06-20
  • 2016-03-29
  • 2017-09-11
  • 1970-01-01
相关资源
最近更新 更多