安卓测试 - 如何添加Espresso日志

7
我正在使用。
androidTestCompile 'com.android.support.test:runner:0.3'
    androidTestCompile 'com.android.support.test:rules:0.3'
    androidTestCompile 'com.android.support:support-annotations:23.0.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
    androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2') {
        // this library uses the newest app compat v22 but the espresso contrib still v21.
        // you have to specifically exclude the older versions of the contrib library or
        // there will be some conflicts
        exclude group: 'com.android.support', module: 'appcompat'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude module: 'recyclerview-v7'
    }
    androidTestCompile 'junit:junit:4.12'

我正在进行测试,想知道是否有一种方法可以让Espresso打印一个日志,显示测试的开始和结束,这样如果出现问题,就可以很容易地查看logcat。

这将与Papertrail混合使用,以检查为什么测试失败以及在哪里失败,尤其适用于私有集成服务器。


它已经打印日志了... TestRunner I started: successCloseView - Daniel Gomez Rico
3
真的没有办法在 Espresso 测试中添加调试日志/控制台条目吗?当现有输出不足够时,这是自然的下一步:增加更多的日志记录。 - ChrisPrime
4个回答

8

3

我检查了框架的代码,看起来它使用的是System.out.println,在测试中也可以工作。不幸的是,Android日志不能这样做。


3

下次请展示一下自己实现这个功能所采取的步骤。

运行测试后,您可以查看logcat。一些辅助类已经记录在logcat中。例如,请查看android.support.test.internal.runner.lifecycle.ActivityLifecycleMonitorImpl。它会记录活动的生命周期状态:

D/LifecycleMonitor: Lifecycle status change:com.example.android.MyActivity@f3c7955 in: STOPPED

在测试类中,任何你的类内部的日志语句都会被显示。最后,在你的测试类内部的任何日志语句都会被显示。

@RunWith(AndroidJUnit4.class)
public class SomeTest {

    @Before
    public void setUp() throws Exception {
        Log.d(TAG, "setup");
    }

    ...
}

我能够在Android Studio和命令行中查看日志。

最后,我想说如果使用Timber时也能使日志正常工作。


没错。只需使用 Log.d 即可正常工作。我可以在 Android Studio 的“Android Monitor”中看到日志记录。 - rpattabi

0

我建议使用TestWatcher并设置自己的Logger和TAG进行过滤。

将TestWatcher分配为测试规则。我个人喜欢使用Rule Chain,因为我会有几个TestWatchers(例如:在失败时捕获屏幕截图)。

@get:Rule
val chain: RuleChain = RuleChain
    .outerRule(ScenarioRule())
    .around(ScreenshotRule())

在我的场景规则中,当测试开始和结束时,我将使用自定义记录器打印输出:
import org.junit.rules.TestWatcher
import org.junit.runner.Description

class ScenarioRule: TestWatcher() {

    var testLog = TestLog()

    override fun starting(description: Description) {
        testLog.info("Starting: ${description.displayName}")
    }

    override fun finished(description: Description) {
        testLog.info("Finished: ${description.displayName}")
    }
}

我的自定义测试记录器的片段:

import android.util.Log
const val TAG = "AUTOMATION"

class TestLog {

    fun info(message: String) {
        Log.i(TAG, "$message")
    }
    fun warn(message: String) {
        Log.w(TAG, "$message")
    }

然后在Logcat中基于您的标签创建一个新的过滤器

2021-11-19 15:54:22.241 11621-11673/com.example I/AUTOMATION: Starting: loginWithCredentials(com.example.tests.LoginFlowUITest)
2021-11-19 15:54:35.669 11621-11673/com.example I/AUTOMATION: Finished: loginWithCredentials(com.example.tests.LoginFlowUITest)

继续添加您想要的详细信息并重复使用此日志记录器以进行测试步骤。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接