在android studio junit测试中记录消息 [英] Log messages in android studio junit test

查看:118
本文介绍了在android studio junit测试中记录消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android Studio中运行JUnit(方法)测试时,是否可以打印出Logcat(Log.i,Log.d)消息?

我可以看到System.out.print消息,但没有logcat打印输出.

在运行配置(Android Studio的GUI窗口)中,有用于Android测试下的测试的logcat选项,而对于JUnit测试则没有.

这有可能吗?感谢您的提示!

解决方案

由于Logcat是Android功能,因此在单元测试中将看不到Logcat输出-JUnit测试只能使用标准Java,因此Android功能将无法使用.

在单元测试中,您可以做的是将测试倍数"注入经过测试的组件中.但是Log.x调用是静态的,因此您无法覆盖它们(不解析为PowerMock,您应该不惜一切代价避免使用此方法).

因此,第一步将是引入一个非静态类,该类将充当Log.x调用的代理:

/**
 * This class is a non-static logger
 */
public class Logger {

    public void e(String tag, String message) {
        Log.e(tag, message);
    }

    public void w(String tag, String message) {
        Log.w(tag, message);
    }

    public void v(String tag, String message) {
        Log.v(tag, message);
    }

    public void d(String tag, String message) {
        Log.d(tag, message);
    }
}

现在在您有Log.x个电话的每个地方使用此类.

第二步是编写Logger的测试双重实现,该实现将重定向到标准输出:

public class UnitTestLogger extends Logger{

    @Override
    public void e(String tag, String message) {
        System.out.println("E " + tag + ": " + message);
    }

    // similar for other methods
}

最后一步是在单元测试中注入UnitTestLogger而不是Logger:

@RunWith(MockitoJUnitRunner.class)
public class SomeClassTest {

    private Logger mLogger = new UnitTestLogger();

    private SomeClass SUT;

    @Before
    public void setup() throws Exception {
        SUT = new SomeClass(/* other dependencies here */ mLogger);
    }

}

如果要严格限制OOP概念,可以提取LoggerUnitTestLogger的通用接口.

也就是说,我从来没有遇到需要研究单元测试中的Log.x调用的情况.我怀疑您也不需要它.您可以在调试模式下运行单元测试,并在调试器中逐行检查代码,这比尝试调查logcat输出要快得多.

一般建议:

如果您要测试的代码包含Log.x静态调用,并且您的单元测试不会崩溃-您有问题.

我猜想所有的测试都在使用Robolectric运行,或者您在build.gradle中有以下语句:unitTests.returnDefaultValues = true.

如果使用Robolectric运行所有测试,则效率低下,但是如果所有Android调用均返回默认值,则测试套件将不可靠.我建议您先解决此问题,然后再继续进行操作,因为将来它会以某种方式咬住您.

Is there a way to print out Logcat (Log.i, Log.d) messages when running a JUnit (method) test in Android Studio?

I can see System.out.print message but no logcat printouts.

In the runconfiguration (GUI window of Android Studio) there are logcat options for tests under Android tests but not for JUnit tests.

Is this possible somehow? Thanks for any hints!

解决方案

Logcat output will not be seen in unit tests because Logcat is Android feature - JUnit tests can use only standard Java, therefore Android features won't work.

What you can do in unit tests is inject "test doubles" into tested components. But Log.x calls are static, so you can't override them (without resolving to e.g. PowerMock, which you should avoid at all costs).

Therefore, the first step will be to introduce a non-static class that will behave as a proxy for Log.x calls:

/**
 * This class is a non-static logger
 */
public class Logger {

    public void e(String tag, String message) {
        Log.e(tag, message);
    }

    public void w(String tag, String message) {
        Log.w(tag, message);
    }

    public void v(String tag, String message) {
        Log.v(tag, message);
    }

    public void d(String tag, String message) {
        Log.d(tag, message);
    }
}

Use this class in every place you have Log.x calls now.

Second step would be to write a test-double implementation of Logger that redirects to standard output:

public class UnitTestLogger extends Logger{

    @Override
    public void e(String tag, String message) {
        System.out.println("E " + tag + ": " + message);
    }

    // similar for other methods
}

The last step is to inject UnitTestLogger instead of Logger in unit tests:

@RunWith(MockitoJUnitRunner.class)
public class SomeClassTest {

    private Logger mLogger = new UnitTestLogger();

    private SomeClass SUT;

    @Before
    public void setup() throws Exception {
        SUT = new SomeClass(/* other dependencies here */ mLogger);
    }

}

If you want to be rigorously strict about OOP concepts, you can extract common interface for Logger and UnitTestLogger.

That said, I never encountered a need to investigate Log.x calls in unit tests. I suspect that you don't need it either. You can run unit tests in debug mode and step over the code line-by-line in debugger, which is much faster than attempting to investigate logcat output...

General advice:

If the code you are testing contains Log.x static calls and your unit tests do not crash - you have a problem.

I'd guess that either all tests are being run with Robolectric, or you have this statement in build.gradle: unitTests.returnDefaultValues = true.

If you run all the tests with Robolectric, then it is just unefficient, but if all Android calls return default values, then you test suite is not reliable. I suggest you fix this issue before proceeding any further because it will bite you in the future one way or another.

这篇关于在android studio junit测试中记录消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆