Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Tests - How to add Espresso Logs

I´m using

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'

for tests and was wondering if there´s a way to say to espresso to print a log with the started test and the finished test so will be easy to see the logcat if something happens.

That will be great to mix with papertrail to check why tests are failing and where with private integration servers.

like image 568
Daniel Gomez Rico Avatar asked Sep 10 '15 15:09

Daniel Gomez Rico


People also ask

How do I record Espresso test on Android?

Record UI interactionsClick Run > Record Espresso Test. In the Select Deployment Target window, choose the device on which you want to record the test. If necessary, create a new Android Virtual Device. Click OK.


2 Answers

You can capture Android Logs by running tests on your CI server through Spoon. See how-to examples on their website and check out the links below for example reports and then the logs for one of the tests. Each test run has the logs captured and saved in the report.

Report Output: http://square.github.io/spoon/sample/index.html

Example Logs: http://square.github.io/spoon/sample/logs/0403681E12013007/com.example.spoon.ordering.tests.OrderActivityTest/testMakeASandwich_ItTastesGood.html

like image 174
Sam Edwards Avatar answered Oct 31 '22 19:10

Sam Edwards


Next time please show us what steps you took to get this working yourself.

You can just look in logcat after running tests. Some of the helper classes already log to logcat. For example look at android.support.test.internal.runner.lifecycle.ActivityLifecycleMonitorImpl. It logs activity lifecycle states:

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

Any log statements inside your classes under test will also get displayed. Finally any log statements inside your test classes will get displayed.

@RunWith(AndroidJUnit4.class)
public class SomeTest {

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

    ...
}

I'm able to see the logs in both Android Studio and the command line.

I'll wrap up by saying that I'm also able to get the logging to work if when using Timber instead.

like image 24
tir38 Avatar answered Oct 31 '22 21:10

tir38