12

I have created simple test in Android Studio. It just prints hello from test and compares 1 with 5

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

I run it but don't see hello from test anywhere.

The output:

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

Where to System.out.println does actually print

Update

I tried to use Log.d("MyApp", "hello from test"); but result is the same.

I tried to search in different sub-windows by hello from test but nothing was found.

Update 2

I changed code to use 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);
    }
}

Screenshots:

Test is done. Running test

But logcat is empty. empty logcat

Community
  • 1
  • 1
Maxim Yefremov
  • 13,671
  • 27
  • 117
  • 166

2 Answers2

6

Use Log to print out strings and check them in your logcat.

There are different variants you can base on your need like Log.d, Log.i, Log.e, etc which are for different purposes like debug, info, error, etc.

And make sure the proper 'Log level' for your logcat is selected. If it's on Error you won't see the output for Log.d.

Further read

enter image description here enter image description here

Samurai
  • 3,724
  • 5
  • 27
  • 39
  • I tried `Log.e` so with any `Log level` I must see my message, but I do not see it. – Maxim Yefremov May 03 '15 at 04:51
  • If that's the issue then I can only imagine that the part of code has somehow never reached (or the build fails or something). If the app runs then just try `Log.e` on your first activity right after `setContentView` just for test. – Samurai May 03 '15 at 05:12
  • If Android Studio throws "cannot find symbol variable log" do not forget to add at the top of your class the import android.util.Log; – Joseph Mar 07 '22 at 17:39
0

This problem happens when the class is in the wrong test folder!, Change to the folder you have (test). Your class is possibly in the folder (AndroidTest)

Tacila
  • 75
  • 1
  • 4