Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to print logs in cmd console while execute android instrument test

I open a cmd on windows system, and then input "adb shell am instrument -w com.demo.uia.test/android.support.test.runner.AndroidJUnitRunner" to run android test.

I want to print log in the cmd while run the test, can anyone tell me how to write code to print log? I have tied system.out.println("xx") and Log.i("xx","xx"), but it's useless.

I want to show the log in cmd line, not in logcat. I solved the problem now, use sendStatus api.

like image 362
MoMo Avatar asked Apr 05 '16 11:04

MoMo


People also ask

How to run android tests from command line?

To run a test from the command line, run adb shell to start a command line shell on your device or emulator. Inside that shell you can interact with the activity manager using the am command and use its instrument subcommand to run your tests.

How to run instrumentation test in android?

In order to run the instrumentation test, you need to select Android Instrumentation Tests under Test Artifact in the Build Variants window. You should see the project structure change and the classes under the androidTest folder should now be visible.


1 Answers

I guess @MoMo did something like this to have logging output in command prompt window:

In your test class define a function:

private void out(String str) {
    Bundle b = new Bundle();
    b.putString(Instrumentation.REPORT_KEY_STREAMRESULT, "\n" + str);
    InstrumentationRegistry.getInstrumentation().sendStatus(0, b);
}

Then simply use it in @Test functions like:

out("Some message...");

The output appears when running the test in Command Prompt window with adb shell am instrument..., but it is not shown in Run window of Android Studio. It somehow filters the output of test runs, and I cannot find a setting to modify this filter.

like image 182
gregko Avatar answered Oct 15 '22 17:10

gregko